php PHP用POST数据打开另一个网页
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/311242/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP open another webpage with POST data
提问by thornate
I'm new to PHP and I'm trying to do something that may be bad practise and may well be impossible. I'm basically just hacking something together to test my knowledge and see what PHP can do.
我是 PHP 的新手,我正在尝试做一些可能是不好的做法,而且很可能是不可能的。我基本上只是拼凑一些东西来测试我的知识,看看 PHP 能做什么。
I have one webpage with a form that collects data. That is submited to a PHP script that does a bunch of processing - but doesn't actually display anything important. What I want is that once the processing is done, the script then tells the browser to open another page, where the results are displayed.
我有一个带有收集数据表单的网页。这被提交给一个 PHP 脚本,该脚本进行了一系列处理 - 但实际上并没有显示任何重要的内容。我想要的是,一旦处理完成,脚本就会告诉浏览器打开另一个页面,在那里显示结果。
I know I can use header('Location: page.php');but I can't work out how to provide POST data with this. How can I do that? Alternatively, is there another way to tell the browser to open another page?
我知道我可以使用header('Location: page.php'); 但我不知道如何提供 POST 数据。我怎样才能做到这一点?或者,是否有另一种方法可以告诉浏览器打开另一个页面?
EDIT: What I'm taking from the responses is that it's possibleto do this using various hacks but I'd be better off to just have the processing and the display code in one file. I'm happy with that; this was an experiment more than anything.
编辑:我从回复中得到的是,可以使用各种技巧来做到这一点,但我最好将处理和显示代码放在一个文件中。我很高兴;这比什么都重要。
采纳答案by Josh Smeaton
Is it really necessary to call another page after the processing is done? I'd probably do the following:
处理完成后真的需要调用另一个页面吗?我可能会做以下事情:
<form method="post" action="display.php">
...
</form>
display.php:
显示.php:
if ($_POST) {
require_once(process.php);
process($_POST);
display_results;
}
with process.php containing the code necessary for processing the post request.
process.php 包含处理 post 请求所需的代码。
Alternatively, you could use something like the cURL library to pass the results of the processing to a page specified by yourself. Don't know if that's really what you're after though.
或者,您可以使用 cURL 库之类的东西将处理结果传递到您自己指定的页面。不知道这是否真的是你所追求的。
回答by Tom Haigh
You could store that data in the session e.g. in the first file that handles the post
您可以将该数据存储在会话中,例如在处理帖子的第一个文件中
session_start();
$_SESSION['formdata'] = $_POST; //or whatever
then you can read it on the next page like
然后你可以在下一页阅读它
session_start();
print_r($_SESSION['formdata']);
or you could pass it through GET: (but as per comments this is a bad idea)
或者您可以通过 GET 传递它:(但根据评论,这是一个坏主意)
header('Location: page.php?' . http_build_query($_POST));
If you do that make sure you do additional processing/validation on page.php as a malicious user could change the variables. also you may not need the whole post transmitted to the next page
如果这样做,请确保在 page.php 上进行额外的处理/验证,因为恶意用户可能会更改变量。您也可能不需要将整个帖子传输到下一页
Edit
编辑
I should make clear that I think the second option is possibly worse, as you are limited by the size of data you can send through get and it is possibly less secure as users can more obviously manipulate the data.
我应该明确指出,我认为第二个选项可能更糟,因为您受到可以通过 get 发送的数据大小的限制,并且可能不太安全,因为用户可以更明显地操纵数据。
回答by Tom
You could use JavaScript as a dirty work-around:
您可以使用 JavaScript 作为一种肮脏的解决方法:
<form id="redirect_form" method="post" action="http://someserver.com/somepage.php">
<input type="hidden" name="field_1" value="<?php echo htmlentities($value_1); ?>">
<input type="hidden" name="field_2" value="<?php echo htmlentities($value_2); ?>">
<input type="hidden" name="field_3" value="<?php echo htmlentities($value_3); ?>">
</form>
<script type="text/javascript">
document.getElementById('redirect_form').submit();
</script>
(the script should be below the form)
(脚本应在表单下方)
回答by user10117
Do it all in one script and just output different HTML for the results.
在一个脚本中完成所有操作,只需为结果输出不同的 HTML。
<?php if($doingForm) { ?>
html for form here
<?php } else { ?>
html for results
<? } ?>
回答by DisasterMan
This problem has vexed me for some time. My custom CMS does some quite complex processing, uploading and manipulation, and so sometimes ouputs quite lengthy error and information messages, which aren't suitable for converting to GET data, and I have always wanted to avoid the reload problem on data INSERT, but not yet found an adequate solution.
这个问题困扰了我一段时间。我的自定义 CMS 做了一些相当复杂的处理、上传和操作,所以有时会输出很长的错误和信息消息,不适合转换为 GET 数据,我一直想避免数据插入时的重新加载问题,但是还没有找到合适的解决方案。
I believe the correct way to go about this, is to create message arrays for each possible state - each message or error you could want to display, and then you only need to send error/message numbers which are a lot easier to handle than long data strings, but it's something I have always shied away from personally as I find it a bit tedious and cumbersome. Frankly, this is probably just laziness on my part.
我相信解决这个问题的正确方法是为每个可能的状态创建消息数组 - 您可能想要显示的每个消息或错误,然后您只需要发送错误/消息编号,这些编号比长处理容易得多数据字符串,但这是我个人一直回避的事情,因为我觉得它有点乏味和麻烦。坦率地说,这可能只是我的懒惰。
I quite like the SESSION variable storage solution, but this raises the question of how do you ensure the SESSION data is properly destroyed? As long as you ensure you are only sending information (messages/errors) and not data that should/could be stored (and thus potentially sensitive) this should be an avoidable problem.
我非常喜欢 SESSION 变量存储解决方案,但这提出了如何确保 SESSION 数据被正确销毁的问题?只要您确保您只发送信息(消息/错误)而不是应该/可以存储的数据(因此可能是敏感的),这应该是一个可以避免的问题。
回答by Gareth
There's no way to redirect the user's browser to an arbitary page and sent a POST request. That would be a bit security risk, where any link could cause you to make any form submission to an arbitrary site without you having any kind of clue about what was going to happen.
无法将用户的浏览器重定向到任意页面并发送 POST 请求。这会带来一些安全风险,任何链接都可能导致您向任意站点提交任何表单,而您对将要发生的事情一无所知。
In short, it's not possible
简而言之,这是不可能的
回答by activout.se
AFAIK this is usually done as a two-step process:
AFAIK 这通常分为两个步骤:
- On form.php, POST the data to script process.php
- The process.php script processes the data but never outputs anything itself, it always calls header("Location: asdasd") to redirect to a success.php or failure.php page (if applicable)
- 在 form.php 上,将数据 POST 到脚本 process.php
- process.php 脚本处理数据但本身从不输出任何内容,它总是调用 header("Location: asdasd") 重定向到 success.php 或 failure.php 页面(如果适用)
回答by NiklasMM
I hope i got your qestion right. You might try this:
我希望我有你的问题。你可以试试这个:
- Adjust your form to look like this:
- 调整你的表格看起来像这样:
form method="POST" action="process_data.php"
表单方法=“POST”动作=“process_data.php”
2.
Then you create the file process_data.php, wich surprisingly processes the data.
And in this file you use header:
For example:
2. 然后你创建文件 process_data.php,它出人意料地处理数据。
在这个文件中你使用标题:
例如:
$head = sprintf("page.php?data1=%d?data2=%d",$data1,$data2);
header($head);
$head = sprintf("page.php?data1=%d?data2=%d",$data1,$data2);
头($头);
I hope i could help.
我希望我能帮上忙。

