Is it possible to send POST data with a PHP redirect?

前端 未结 10 2129
一个人的身影
一个人的身影 2021-02-14 05:04

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 w

10条回答
  •  囚心锁ツ
    2021-02-14 05:45

    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: ');
    

    Basic usage Example:

    page1.html - the page which contains the form

    
      
        

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

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

    $val)
            echo $key . ' = ' . $val . '
    '; } else echo 'variable1 and variable2 are not set';

    * don't try this at home!

提交回复
热议问题