PHP - 使用 header() 传递 POST 变量?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2447211/
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 - Pass POST variables with header()?
提问by Dan
I'm trying to use the header() function to create a redirect. I would like to display an error message. Currently I'm sending the message as a parameter through the URL, however this makes it look quite ugly.
我正在尝试使用 header() 函数来创建重定向。我想显示错误消息。目前我通过 URL 将消息作为参数发送,但这使它看起来很丑陋。
Is there a way to pass this value as a post variable instead?
有没有办法将此值作为后变量传递?
Any advice appreciated.
任何建议表示赞赏。
Thanks.
谢谢。
回答by Seaux
Dan, You could start and store a session in PHP then save the message as a session variable. This saves you from having to transfer the message in an HTTP request.
丹,您可以在 PHP 中启动和存储会话,然后将消息保存为会话变量。这使您不必在 HTTP 请求中传输消息。
Manipulating Sessions
操作会话
//Start the session
session_start();
//Dump your POST variables
$_SESSION['POST'] = $_POST;
//Redirect the user to the next page
header("Location: bar.php");
Now, within bar.phpyou can access those POST variables by re-initiating the session.
现在,bar.php您可以通过重新启动会话来访问这些 POST 变量。
//Start the session
session_start();
//Access your POST variables
$temp = $_SESSION['POST'];
//Unset the useless session variable
unset($_SESSION['POST']);
To read more about sessions, check out: http://php.net/manual/en/function.session-start.php
要阅读有关会话的更多信息,请查看:http: //php.net/manual/en/function.session-start.php
回答by Sarfraz
The header function is used to send HTTP response headers back to the user so actually you cannot use it to create request headers :(
header 函数用于将 HTTP 响应标头发送回用户,因此实际上您不能使用它来创建请求标头 :(
One possibility is to use the CURLbut I don't think it is worth of what you are doing.
一种可能性是使用 CURL,但我认为这不值得你在做什么。
回答by the-banana-king
Provided that you have local access to the page displaying the error, instead of redirecting you could include it in the page which caused the error and then programmatically display the error message.
如果您可以本地访问显示错误的页面,您可以将其包含在导致错误的页面中,然后以编程方式显示错误消息,而不是重定向。
if(something_went_wrong()) {
require_once('errors.php');
display_error('something really went wrong.');
}
The errors.phpfile would then contain a definition for display_error($message), which displays the formatted message.
errors.php然后该文件将包含 的定义display_error($message),它显示格式化的消息。
回答by Sam
When passing variables between modules I have found it easier to construct an array from the variables, convert the array to json and store it in a db table with two columns, a varchar key and a text data. The json would go in data and the key could be anything you want. Then in the target module you just read that back, convert the json back to an array and voila, you have your variables. No $_POST, no $_SESSION, no fuss, no muss, quick and easy. Of course that assumes you have access to a database, although you could use a file on the server. $_POST is useless since it needs a and $_SESSION can be cranky and can lead to unexpected results. Otherwise you'd almost have to use ajax.
在模块之间传递变量时,我发现从变量构造数组、将数组转换为 json 并将其存储在具有两列、一个 varchar 键和一个文本数据的 db 表中更容易。json 将进入数据,密钥可以是您想要的任何内容。然后在目标模块中,您只需将其读回,将 json 转换回数组,瞧,您就有了变量。没有 $_POST,没有 $_SESSION,没有大惊小怪,没有麻烦,快速而简单。当然,前提是您可以访问数据库,尽管您可以使用服务器上的文件。$_POST 是无用的,因为它需要一个,而 $_SESSION 可能会胡思乱想,并可能导致意想不到的结果。否则你几乎不得不使用ajax。

