PHP 标头位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12213480/
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 Header Location
提问by Vijay
Possible Duplicate:
PHP header() redirect with POST variables
I'm writing a script where form data is being submitted to another script. I want the second script to perform some error-checking on the submitted $_POSTdata, and if everything is okay, process the data. If the data has errors, I'm using header('Location: http://www.example.com/script.php');to return the visitor back to the form page.
我正在编写一个脚本,其中表单数据被提交给另一个脚本。我希望第二个脚本对提交的$_POST数据执行一些错误检查,如果一切正常,则处理数据。如果数据有错误,我使用header('Location: http://www.example.com/script.php');将访问者返回到表单页面。
The issue I'm having is that I want the form to be sticky - fields with correct data maintain the values the user put in them. Obviously, to get those values, I need access to the $_POSTarray. However, this seems to be destroyed when the header()call forwards the visitor back to the form.
我遇到的问题是我希望表单具有粘性 - 具有正确数据的字段会维护用户输入的值。显然,要获得这些值,我需要访问$_POST数组。但是,当header()调用将访问者转发回表单时,这似乎被破坏了。
Is there any way to use the header Location stuff to redirect the visitor to another page, while still preserving the $_POSTdata?
有什么方法可以使用标题 Location 内容将访问者重定向到另一个页面,同时仍然保留$_POST数据?
Now I'm using like this : header('Location: http://www.example.com/add.php?id='.$id.'&name='.$name.'&code='.$code.'&desc='.$description.'');and accessing as $_GET.
现在我像这样使用 :header('Location: http://www.example.com/add.php?id='.$id.'&name='.$name.'&code='.$code.'&desc='.$description.'');并访问 as $_GET。
For the same can I use and kind of POST in header('location: xxx')??
同样,我可以在header('location: xxx')??
回答by Mic1780
the best way to achieve this 'sticky form' is to use a session.
实现这种“粘性表单”的最佳方法是使用会话。
<?php
session_start();
$_SESSION = $_POST;
//do error checking here
//if all is valid
session_write_close();
header('Location: *where you want your form to go*');
die;
?>
and on the redirect page you can use them like so:
在重定向页面上,您可以像这样使用它们:
<?php
session_start();
//use $_SESSION like you would the post data
?>
回答by Dunhamzzz
You could store the $_POSTdata in a session:
您可以将$_POST数据存储在会话中:
session_start();
$_SESSION['submitted_data'] = $_POST;
And then load the values into the inputs by loading them from the $_SESSION['submitted_data']variable, just remember to have session_start()at the top of your error page too.
然后通过从$_SESSION['submitted_data']变量加载值来将值加载到输入中,只需记住也session_start()将其放在错误页面的顶部。
回答by sanmai
Use sessionsfor that. In the form page:
为此使用会话。在表单页面中:
<?php
if (!empty($_COOKIE[session_name()])) {
// we only start session if there is a session running
session_id() || session_start();
}
if (empty($_POST) && !empty($_SESSION['POST'])) {
// make sure you're not overwriting
$_POST = $_SESSION['POST'];
}
// and so on just like you have $_POST filled
In a script that receives $_POST data:
在接收 $_POST 数据的脚本中:
<?php
// after you're done with checking and stuff
session_id() || session_start();
$_SESSION['POST'] = $_POST;
header('Location: /script.php');
Both scripts should be on the save domain for sessions to work. If you're using relative URI in the Locationheader, then everything should work fine.
两个脚本都应该在保存域上,以便会话工作。如果您在Location标头中使用相对 URI ,则一切正常。

