带有 Post 参数的 PHP 重定向 [英] PHP Redirection with Post Parameters

查看:44
本文介绍了带有 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:

实际上,这意味着在 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 重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆