PHP header location

Possible duplicate:
PHP header () redirects POST variables

I am writing a script where form data is sent to another script. I want the second script to do some error checking on the presented data $_POST, and if everything is ok, process the data. If the data has errors, I use header('Location: http://www.example.com/script.php');to return the visitor to the form page.

The problem I'm facing is that I want the form to be sticky - fields with the correct data saved the values ​​that the user entered into them. Obviously, to get these values, I need access to the array $_POST. However, this seems to be ruined when the call header()redirects the visitor back to the form.

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

Now I use it like this: header('Location: http://www.example.com/add.php?id='.$id.'&name='.$name.'&code='.$code.'&desc='.$description.'');and access as $_GET.

For the same, you can use the POST view in header('location: xxx')?

+5
source share
3 answers

the best way to achieve this “sticky form” is to use a session.

<?php
session_start();
$_SESSION = $_POST;
//do error checking here
//if all is valid
session_write_close();
header('Location: *where you want your form to go*');
die;
?>

and on the redirect page you can use them like this:

<?php
session_start();
//use $_SESSION like you would the post data
?>
+15
source

You can save data $_POSTin a session:

session_start();
$_SESSION['submitted_data'] = $_POST;

And then load the values ​​into the inputs, loading them from a variable $_SESSION['submitted_data'], just remember that there is also an error at the top of the page session_start().

+2
source

sessions. :

<?php
if (!empty($_COOKIE[session_name()])) {
    // we only start session if there is a session running
    session_id() || session_start();
}

if (empty($_POST) && !empty($_SESSION['POST'])) {
    // make sure you're not overwriting
    $_POST = $_SESSION['POST'];
}
// and so on just like you have $_POST filled

script, $_POST:

<?php
// after you're done with checking and stuff
session_id() || session_start();
$_SESSION['POST'] = $_POST;
header('Location: /script.php');

, . URI Location, .

0
source

All Articles