PHP Redirection with Post Parameters(带有 Post 参数的 PHP 重定向)
问题描述
我有一个网页.此网页或多或少通过以下方式将用户重定向到另一个网页:
I have a webpage. This webpage redirects the user to another webpage, more or less the following way:
<form method="post" action="anotherpage.php" id="myform">
<?php foreach($_GET as $key => $value){
echo "<input type='hidden' name='{$key}' value='{$value}' />";
} ?>
</form>
<script>
document.getElementById('myform').submit();
</script>
好吧,你看,我所做的是将 GET 参数转换为 POST 参数.不要告诉我它不好,我自己知道,这并不是我真正做的事情,重要的是我从数组中收集数据并尝试通过 POST 将其提交到另一个页面.但是如果用户关闭了 JavaScript,它将无法工作.我需要知道的是:有没有办法通过 PHP 传输 POST 参数,以便重定向也可以通过 PHP 方式完成 (header('Location: anotherpage.php');
)?
Well, you see, what I do is transferring the GET params into POST params. Do not tell me it is bad, I know that myself, and it is not exactly what I really do, what is important is that I collect data from an array and try submitting it to another page via POST. But if the user has JavaScript turned off, it won't work. What I need to know: Is there a way to transfer POST parameters by means of PHP so the redirection can be done the PHP way (header('Location: anotherpage.php');
), too?
通过 POST 传递参数对我来说非常重要.我无法使用 $_SESSION 变量,因为该网页位于另一个域中,因此 $_SESSION 变量不同.
It is very important for me to pass the params via POST. I cannot use the $_SESSION variable because the webpage is on another domain, thus, the $_SESSION variables differ.
不管怎样,我只需要一种用 PHP 传输 POST 变量的方法 ^^
Anyway, I simply need a way to transfer POST variables with PHP ^^
提前致谢!
推荐答案
您可以通过标头重定向 POST 请求,并包含 POST 信息.但是,您需要显式返回 HTTP 状态代码 307.浏览器将 302 视为使用 GET 重定向,忽略原始方法.这在 HTTP 文档中明确指出:
You CAN header redirect a POST request, and include the POST information. However, you need to explicitly return HTTP status code 307. Browsers treat 302 as a redirect with for GET, ignoring the original method. This is noted explicitly in the HTTP documentation:
- https://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.3.8
实际上,这意味着在 PHP 中您需要在重定向位置之前设置状态代码:
Practically, this means in PHP you need to set the status code before the redirect location:
header('HTTP/1.1 307 Temporary Redirect');
header('Location: anotherpage.php');
但是,请注意,根据 HTTP 规范,用户代理必须询问用户是否可以将 POST 信息重新提交到新 URL.实际上,Chrome 不会询问,Safari 也不会询问,但 Firefox 会向用户显示一个确认重定向的弹出框.根据您的操作限制,这可能没问题,但在一般使用情况下,它肯定有可能给最终用户造成混淆.
However, note that according to the HTTP specification, the user agent MUST ask user if they are ok resubmitting the POST information to the new URL. In practical terms, Chrome doesn't ask, and neither does Safari, but Firefox will present the user with a popup box confirming the redirection. Depending on your operating constraints, maybe this is ok, although in a general usage case it certainly has the potential to cause confusion for end users.
这篇关于带有 Post 参数的 PHP 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:带有 Post 参数的 PHP 重定向
基础教程推荐
- Doctrine 2 - 在多对多关系中记录更改 2022-01-01
- 在 CakePHP 2.0 中使用 Html Helper 时未定义的变量 2021-01-01
- 使用 PDO 转义列名 2021-01-01
- 如何在 Symfony 和 Doctrine 中实现多对多和一对多? 2022-01-01
- 如何在 XAMPP 上启用 mysqli? 2021-01-01
- phpmyadmin 错误“#1062 - 密钥 1 的重复条目‘1’" 2022-01-01
- 在 yii2 中迁移时出现异常“找不到驱动程序" 2022-01-01
- PHP 守护进程/worker 环境 2022-01-01
- 找不到类“AppHttpControllersDB",我也无法使用新模型 2022-01-01
- HTTP 与 FTP 上传 2021-01-01