Forum Moderators: coopster

Message Too Old, No Replies

Preserve $_POST data through header(Location: .)

It seems like this should be basic, but I can't find how to do it

         

MatthewHSE

12:33 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm writing a script where form data is being submitted to another script. I want the second script to perform some error-checking on the submitted $_POST data, and if everything is okay, process the data. If the data has errors, I want to use
header(Location: http://www.example.com/script.php);
to return the visitor back to the form page.

So far so good, I can handle that without any problem. The issue I'm having is that I want the form to be sticky - fields with correct data maintain the values the user put in them. Obviously, to get those values, I need access to the $_POST array. However, this seems to be destroyed when the

header(Location)
section forwards the visitor back to the form.

Is there any way to use the header Location stuff to redirect the visitor to another page, while still preserving the $_POST data?

dreamcatcher

8:35 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Only way I can think of is assigning your post data to a query string when you redirect:

$data = $_POST['data'];

header("Location: index.php?data=$data");

Then access it as a $_GET variable when it redirects.

dc

Angelis

8:46 am on Nov 15, 2005 (gmt 0)

10+ Year Member



That will only work if the receiving server has register globals turned on.

jd01

10:41 am on Nov 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



GET will work if the receiving script is set to receive the variable sent, even if globals are turned off...

The only way I know of to POST is to set the headers and open the file:

$header .= "POST /path-to/the-file HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($yourvariables) . "\r\n\r\n";
$fp = fsockopen ('www.example.com', 80, $errno, $errstr, 30);

There is quite a bit more information on the fsockopen manual [us3.php.net] page.

Justin

BTW Unless it is explicitly stated on a form/webpage, posting (or sending any information) to a remote server/site is considered bad practice and a security risk for users.