简体   繁体   English

PHP用POST数据打开另一个网页

[英]PHP open another webpage with POST data

I'm new to PHP and I'm trying to do something that may be bad practise and may well be impossible. 我是PHP的新手,我正在尝试做一些可能不好的事情并且可能是不可能的。 I'm basically just hacking something together to test my knowledge and see what PHP can do. 我基本上只是在一起黑客攻击测试我的知识,看看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. 这是一个PHP脚本,它执行一堆处理 - 但实际上并没有显示任何重要的东西。 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. 我想要的是,一旦处理完成,脚本就会告诉浏览器打开另一个页面,显示结果。

I know I can use header('Location: page.php'); 我知道我可以使用标题('Location:page.php'); but I can't work out how to provide POST data with this. 但我无法弄清楚如何提供POST数据。 How can I do that? 我怎样才能做到这一点? Alternatively, is there another way to tell the browser to open another page? 或者,是否有另一种方法告诉浏览器打开另一个页面?

EDIT: What I'm taking from the responses is that it's possible to 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. 这个实验比什么都重要。

You could store that data in the session eg 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. 如果这样做,请确保在page.php上执行其他处理/验证,因为恶意用户可能会更改变量。 also you may not need the whole post transmitted to the next page 您也可能不需要将整个帖子传输到下一页

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发送的数据大小受到限制,并且可能不太安全,因为用户可以更明显地操纵数据。

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: Display.php的:

if ($_POST) {
    require_once(process.php);
    process($_POST);
    display_results;
}

with process.php containing the code necessary for processing the post request. with 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. 或者,您可以使用类似cURL库的内容将处理结果传递给您自己指定的页面。 Don't know if that's really what you're after though. 不知道这是不是你真正想要的。

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) (脚本应该在表单下面)

There's no way to redirect the user's browser to an arbitary page and sent a POST request. 无法将用户的浏览器重定向到仲裁页面并发送POST请求。 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. 这会有一点安全风险,任何链接都可能导致您将任何表单提交到任意站点,而您不会对将要发生的事情有任何线索。

In short, it's not possible 简而言之,这是不可能的

AFAIK this is usually done as a two-step process: AFAIK这通常分为两个步骤:

  1. On form.php, POST the data to script process.php 在form.php上,将数据POST到脚本process.php
  2. 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) process.php脚本处理数据但从不输出任何内容,它总是调用header(“Location:asdasd”)重定向到success.php或failure.php页面(如果适用)

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

<? } ?>

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数据,我一直想避免数据INSERT上的重载问题,但是尚未找到适当的解决方案。

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? 我非常喜欢SESSION变量存储解决方案,但这引发了一个问题,即如何确保SESSION数据被正确销毁? 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. 只要您确保只发送信息(消息/错误)而不是应该/可以存储的数据(因此可能存在敏感信息),这应该是一个可以避免的问题。

I hope i got your qestion right. 我希望我的问题得到了解决。 You might try this: 你可以试试这个:

  1. Adjust your form to look like this: 将表单调整为如下所示:

form method="POST" action="process_data.php" form method =“POST”action =“process_data.php”

2. Then you create the file process_data.php, wich surprisingly processes the data. 2.然后创建文件process_data.php,令人惊讶地处理数据。
And in this file you use header: 在这个文件中你使用标题:
For example: 例如:

$head = sprintf("page.php?data1=%d?data2=%d",$data1,$data2); $ head = sprintf(“page.php?data1 =%d?data2 =%d”,$ data1,$ data2);
header($head); 报头($头);

I hope i could help. 我希望我能提供帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM