Prashant Singh
Prashant Singh

Reputation: 3793

Redirecting to a different URL using PHP and passing some POST variables (Parameters)

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

Upvotes: 0

Views: 806

Answers (3)

hakre
hakre

Reputation: 197564

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.

Upvotes: 1

Kashif Khan
Kashif Khan

Reputation: 2645

you need to store the POST parameters in SESSION variable

Upvotes: 2

bkzland
bkzland

Reputation: 569

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.

Upvotes: 4

Related Questions