带有Post参数的PHP重定向 [英] PHP Redirection with Post Parameters
问题描述
我有一个网页。此网页或多或少按以下方式将用户重定向到另一个网页:
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传递params非常重要。我不能使用$ _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.
无论如何,我只需要一种方法来传输POST变量PHP ^^
Anyway, I simply need a way to transfer POST variables with PHP ^^
提前致谢!
推荐答案
没有可能直接从服务器执行此操作,因为POST数据应由浏览器发送。
No possibility to do this directly from server, as POST data should be sent by the browser.
但您可以选择其他选择:
But you can choose an alternative :
- 预填充表单自动提交ini你的例子可以工作,但是你写的并不是很好的做法,可以让用户留在空白页面上
- 接收GET参数并用curl(或任何体面的http客户端)将它们发送到第二个站点,然后将结果传输到浏览器。这被称为代理,可能是一个很好的解决方案imho。
- 跨域进行会话共享,这在所有设置上都是不可能的,并且可能很复杂。
设置完成后,会话共享对php代码几乎是透明的。如果您在两个域之间有多个通信需求,则值得这样做。
curl解决方案示例,代码为在域1上运行:
Example with curl solution, code to run on domain 1 :
//retrieve GET parameters as a string like arg1=0&arg1=56&argn=zz
$data = $_SERVER['QUERY_STRING'];
// Create a curl handle to domain 2
$ch = curl_init('http://www.domain2.com/target_script.php');
//configure a POST request with some options
curl_setopt($ch, CURLOPT_POST, true);
//put data to send
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
//this option avoid retrieving HTTP response headers in answer
curl_setopt($ch, CURLOPT_HEADER, 0);
//we want to get result as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//if servers support is and you want result faster, allow compressed response
curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
//execute request
$result = curl_exec($ch);
//show response form domain 2 to client if needed
echo $result;
就是这样,你的客户端的浏览器甚至看不到域2服务器,它只会得到它。知道您是否要将客户端重定向到域,请使用经典的http标头。
That's it, your client's browser won't even see domain 2 server, it will get only result from it. know if you want to redirect client to domain, do it with classic http header.
header('Location: http://www.domain2.com');
当然,这是带有硬编码值的演示代码,还剩2点:
Of course, this is demo code with hardcoded values, and there are 2 point left to you :
- 安全性:应该过滤或重新创建查询字符串以仅传输所需的参数,并且应该断言域2上的服务器返回200个HTTP代码。
- 应用程序逻辑在这一部分需要很少的调整:如果域2应用程序希望在访问者即将到来的同一请求中获取发布数据,则不会这样做。从域2的角度来看,执行POST请求的客户端将是服务器托管域1而不是客户端浏览器,如果客户端IP很重要或者在域2上完成其他客户端检查,这很重要。
如果POST请求用于显示特定于客户端的内容,您还必须进行一些服务器端跟踪,以便将以前发布的数据与重定向的访问者相结合。
希望它更多现在清楚,并将帮助你
Hope it's more clear now and will help you
这篇关于带有Post参数的PHP重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!