views:

120

answers:

3

Hi all. Could someone help me on how to post variables and follow the post to that page?

Regards Phil

A: 

You can't do an HTTP POST request via a redirect. You can do a GET redirect with parameters like this though:

header('Location: target_page.php?a=foo&b=bar');
Jani Hartikainen
+1  A: 

You can use CURL to do that:

$ch = curl_init();
$data = array('name' => 'Foo', 'bar' => 'goo');

curl_setopt($ch, CURLOPT_URL, 'http://myserver.com/post.php');

//post the data
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

//enable RETURN_TRANSFER so curl_exec() returns result of the request
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);

$result = curl_exec($ch);
curl_close($ch);

echo $result;
rubayeet
A: 

if you want to pass data from one page to another and then redirect to that page, you can temporarily stash the data in the $_SESSION superglobal array and then redirect to the new page. once you're there you can then get the data back out of the array and unset() it if need be.

Scott M.