tags:

views:

333

answers:

7

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.

+1  A: 

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.)

fennec
A: 

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: http://stackoverflow.com/questions/471014/code-translation-asp-net-server-transfer-in-php

Langdon
+3  A: 

This does not redirect the browser but it can perform a POST request.

Curl Manual

Curl POST Example

PHP POST Without Curl

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>
Peter Lindqvist
Thanks, though I assure you simple forms came to mind. It needs to be entirely server-side. Apparently it's not possible.
Johannes Gorset
+1  A: 

I'm not sure why you would have any need for this, however it is not possible in any server-side language.

You could use a javascript library such as jQuery to request a page using a post request

Inspire
+5  A: 

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.

symcbean
That'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 -MSFT-
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
A: 

This question was asked here http://stackoverflow.com/questions/653090/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?

Chaim Chaikin
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 Chaikin
A: 

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> 
Sadiqur Rahman