0

How can i redirect to a different page along with passing some POST parameters using PHP ?

3 Answers 3

4

You cannot "redirect" with the POST method per se, what you're after is to execute a POST request to the site you were planning to redirect to. Have a look at the cURL POST example from http://php.net/manual/de/book.curl.php:

$ch = curl_init();

$data = array('name' => 'Foo', 'file' => '@/home/user/test.png');

curl_setopt($ch, CURLOPT_URL, 'http://localhost/upload.php');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

curl_exec($ch);

Substitute the CURLOPT_URL with your target and set the required fields in the $data array. For this to work your PHP needs to have the cURL module enabled.

Alternatively you could store all the data you plan to send in the POST in your session and just have the target read from there.

6
  • I wonder how this works with client information like cookies and language settings.
    – hakre
    Dec 23, 2011 at 16:57
  • You will have to make sure the domain of the page you redirect to is the same as the page you were redirecting from, otherwise you won't be able to read the session and cookie arrays.
    – bkzland
    Dec 23, 2011 at 17:00
  • But if one would already use $_SESSION, why should I write a proxy on my own with curl if the only thing I need to do is a simple redirect?
    – hakre
    Dec 23, 2011 at 17:04
  • You're right, after reading your suggestion to just send the status code, it seems bloated to open a server-side POST-request. But the problem you outline, some browsers changing the type to GET, wouldn't that give a significant amount of control away? I.e., the receiving would have to accept both POST and GET for the same data? Edit: after reading up on the suggested 303, W3C states that it will always be a GET request after redirecting.
    – bkzland
    Dec 23, 2011 at 17:33
  • Exactly, 303 was suggested for a reason. But this has a caveat, it's HTTP 1/1. Sometimes you still get HTTP 1/0 requests.
    – hakre
    Dec 23, 2011 at 17:45
2

you need to store the POST parameters in SESSION variable

1
  • nice idea . I will use this :) Dec 23, 2011 at 16:38
1

How can i redirect to a different page along with passing some POST parameters using PHP?

That is difficult to do, because redirecting needs both, the server and the browser.

Your script can tell the browser that it should redirect.

But the browser, according to the specs, must get confirmation to allow to send the POST request to the redirected URL.

But even so, not all browsers will re-send the post data with the redirect:

Note: When automatically redirecting a POST request after receiving a 301 status code, some existing HTTP/1.0 user agents will erroneously change it into a GET request. (Ref)

So as this does not consistently work and I guess you don't want to have the user to press a button to perform the redirect, you can't create easily what you're looking for with just a redirect.

However as Kashif Khan wrote, you can store the submitted post data into some session and then redirect the user to a new location in which you could read again from that session.

To have this working in the browser nicely, use the 303 See Other status code for the redirect.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.