是否可以使用PHP重定向发送POST数据? [英] Is it possible to send POST data with a PHP redirect?

查看:158
本文介绍了是否可以使用PHP重定向发送POST数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

更新:这不是

解决方案

长话短说:不,不可能使用PHP


我能想到这样做的一种方法是将新表单输出作为隐藏字段输出以及javascript来自动提交表单。 。


这是唯一的方法。






问题是带有POST数据的PHP重定向的可能重复






作为自杀解决方案,你可以考虑使用 HTTP 307 - 临时重定向

来自上面链接的 HTTP / 1.1 文档:


如果307状态代码收到响应GET或HEAD以外的请求,用户代理不得自动重定向请求,除非用户可以确认,因为这可能会改变发出请求的条件。


我的大胆只是强调没有用户确认的重定向实际上取决于浏览器的协议实现。



重定向可以在PHP脚本中实现,该脚本接收 POST 请求,如下所示:

  header('HTTP / 1.1 307临时重定向'); 
标题('位置:<您在外部网站中的页面>');

基本用法示例:



page1.html - 包含表格的页面

 < html> 
< body>
< form method =POSTaction =page2.php>
< input name =variable1/>
< input name =variable2/>
< input type =submit/>
< / form>
< / body>
< / html>

page2.php - 在重定向之前处理POST数据的页面

 <?php 

if(isset($ _ POST ['variable1'])&& ; isset($ _ POST ['variable2'])){

//做你的处理事务

header('HTTP / 1.1 307临时重定向');
header('Location:page3.php');

退出;
} else
echo'variable1和variable2未设置';

page3.php 必须将POST数据重定向到的页面(即外部网站

 <?php 

if (isset($ _ POST ['variable1'])&& isset($ _ POST ['variable2'])){

foreach($ _POST as $ key => $ val)
echo $ key。 '='。 $ val。 <峰; br>;

}其他
echo'variable1和variable2未设置';

* 不要在家试试!


Update: This is NOT a duplicate of How do I send a POST request with PHP?. The solutions there don't work for me, they just output the result of the request, I don't want to do this, I want to send the user to the external site, it is secure website for credit card payment processing.

Update 2: I've added a diagram below to try and clearly explain what I'm trying to do

Here's what I'm trying to do:

  1. a html form is submitted to a php script

  2. the php script does some processing on the data and then submits data via POST request to an external website

  3. I do not want to receive the result of the POST request in my PHP script, instead I just want to send the user to the external site where they see the result of the POST

I'm thinking curl is not suitable for this task as I'm not interested in receiving the result of the request back, I just want to send the user to the next site just as if they submitted the form directly.

To put this another way, I want to submit a form to an external website but I want to submit it to my own script first so I can process the data and send email notifications and then submit onwards to the external website with some new calculated data added.

One way I can think of doing this is to output the new form output as hidden fields along with javascript to auto-submit the form but there has got to be a more straightforward, robust way to do it without relying on javascript. Maybe by manipulating headers, or maybe there is already a php function to simply submit a post request and redirect to the result?

解决方案

Long story short: no, it's not possibile using PHP.

One way I can think of doing this is to output the new form output as hidden fields along with javascript to auto-submit the form ...

It's the only way to do it.


The question is a possible duplicate of PHP Redirect with POST data.


As a suicide solution, you may consider to use HTTP 307 - Temporary Redirect.
From the HTTP/1.1 documentation linked above:

If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.

My bold simply emphasizes that the redirect without the user confirmation actually depends on the browser's implementation of the protocol.

The redirect can be implemented in the PHP script which receives the POST request as follows*:

header('HTTP/1.1 307 Temporary Redirect');
header('Location: <your page in an external site>');

Basic usage Example:

page1.html - the page which contains the form

<html>
  <body>
    <form method="POST" action="page2.php">
      <input name="variable1" />
      <input name="variable2" />
      <input type="submit" />
    </form>
  </body>
</html>

page2.php - the page which processes your POST data before redirect

<?php

if (isset($_POST['variable1']) && isset($_POST['variable2'])) {

  //do your processing stuff

  header('HTTP/1.1 307 Temporary Redirect');
  header('Location: page3.php');

  exit;
} else
    echo 'variable1 and variable2 are not set';

page3.php the page to which the POST data has to be redirected (i.e. the external site)

<?php

if (isset($_POST['variable1']) && isset($_POST['variable2'])) {

    foreach ($_POST as $key=>$val)
        echo $key . ' = ' . $val . '<br>';

} else
    echo 'variable1 and variable2 are not set';

* don't try this at home!

这篇关于是否可以使用PHP重定向发送POST数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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