View Full Version : Supplying a page with $_POST data follwing a server-side redirect

08-13-2008, 11:20 AM
Hi all,

The following example will make the variables x and y available in mypage.php as $_GET data:

header ("Location: mypage.php?x=1&y=2");

Is there a way to send variables to mypage.php using as POST data? (so that they need not be passed as part of the URL).

EDIT: I know I can mimick this using $_SESSION, but I wonder if there is a neat way of incorporating it into a request before the redirect (which ellicits a response) so that I don't have to do it explicitly with $_SESSION.

08-13-2008, 11:32 AM
hidden form and javascript submit instead of a php based header redirect

08-13-2008, 11:41 AM
Yep, thanks, I know that will work, but I would like to do it purely server-side.

08-13-2008, 11:51 AM
I cannot think of a way to do this with just the header() function but you could use cURL to send the POST data.

Then you don't even have to leave the page, i don't know if this is exactly what you were looking for but...

POST Example:

$ch1 = curl_init();

curl_setopt($ch1, CURLOPT_URL,"http://www.example.com/page.php");
curl_setopt($ch1, CURLOPT_POST, 1);
curl_setopt($ch1, CURLOPT_POSTFIELDS,"x=hello&y=goodbye");

curl_exec ($ch1);
curl_close ($ch1);

GET Example:

$ch = curl_init();

curl_setopt ($ch, CURLOPT_URL, "http://www.example.com/page.php?x=hello&y=goodbye");
curl_setopt ($ch, CURLOPT_HEADER, 0);

curl_exec ($ch);

curl_close ($ch);

08-13-2008, 01:18 PM
Hi, yep I had thought of that, but I think the $_SESSION solution is easier. I'll stick with $_SESSION until someone comes up with something else!