How do you go about redirecting a browser and sending a HTTP POST request in PHP? A header("Location: file.php?foo=bar")
of HTTP POST requests, if you will.

- 8,715
- 4
- 36
- 34
-
I don't see why MUST you do that. In fact there must be other more graceful way of solving your problem. – mauris Jan 07 '10 at 13:09
7 Answers
You can't - HTTP does not allow this - if you want to pass arguments via a redirect they have to be embedded into the URL as GET vars.
C.

- 47,736
- 6
- 59
- 94
-
1That's sorta right. You cannot convert a GET into a POST using a HTTP/3xx redirect, but you can redirect a POST to a POST using a HTTP/307. – EricLaw Jan 07 '10 at 15:34
-
No - from rfc2616: "The action required MAY be carried out by the user agent without interaction with the user if and only if the method used in the second request is GET or HEAD" In practice no browser I'm aware of implements the implied get-out clause for this (i.e. asking the user). C. – symcbean Jan 08 '10 at 10:14
-
If the web server receives a GET request and it needs to do a POST request to an another web server as part of the prosessing for the respose, it must do the POST by itself. (For PHP, you probably should use CURL.) HTTP does not provide a method to ask user agent (web browser) to do this for the server. Be warned that GET request is allowed to be repeated at will (because GET is idempotent by definition) and the web server probably should not blindly repeat the POST to another server every time it receives a GET request. – Mikko Rantalainen Oct 20 '11 at 07:33
This does not redirect the browser but it can perform a POST request.
To redirect the browser i'd suggest using Javascript.
An example form that does POST and redirect
<FORM action="http://somesite.com/prog/adduser" method="post">
<P>
<LABEL for="firstname">First name: </LABEL>
<INPUT type="text" id="firstname"><BR>
<LABEL for="lastname">Last name: </LABEL>
<INPUT type="text" id="lastname"><BR>
<LABEL for="email">email: </LABEL>
<INPUT type="text" id="email"><BR>
<INPUT type="radio" name="sex" value="Male"> Male<BR>
<INPUT type="radio" name="sex" value="Female"> Female<BR>
<INPUT type="submit" value="Send"> <INPUT type="reset">
</P>
</FORM>

- 10,122
- 3
- 41
- 60
-
Thanks, though I assure you simple forms came to mind. It needs to be entirely server-side. Apparently it's not possible. – Johannes Gorset Jan 07 '10 at 13:24
I don't believe you can get a browser to POST data by redirecting it in the middle of a request. You're limited to GET. If you want a browser to POST something you need to construct a <form>
and submit it. (Or use an AJAX request.)
PHP doesn't have anything like this. To fulfill your example, you can just simply say $_GET['foo'] = 'bar'; include("file.php")
, however the URL given to the browser will not be changed.
Similar question: Code Translation: ASP.NET Server.Transfer in PHP
This question was asked here How do you POST to a page using the PHP header() function?.
Someone commented that if you already had the data why do you need to post it anywhere, why can't you just act on the data in that page?
-
One reason you may want to post data you already have is to an external website. For example you may want to post data directly to PayPal instead of making the user submit an additional form. – Chaim Jan 07 '10 at 20:13
If you need to transfer data when you redirect without showing data in the URL, you can use $_SESSION, first store data into session then redirect the page, after redirection get data from session and destroy the session data..
OK, if you need to transfer data to other site without showing in the URL, u have to use Javascript instead... like transfer data to payPal. just make a form and write a javascript code to submit the form automatically on page load. below is the sample code:
<form name="myform" action="handle-data.php">
Search: <input type='text' name='query' />
<a href="javascript: submitform()">Search</a>
</form>
<script type="text/javascript">
function submitform()
{
document.myform.submit();
}
</script>

- 141
- 3
- 9