Page 1 of 1
Form variables and sessions
Posted: Fri Apr 19, 2002 6:23 am
by Tony Brush
Hi - 2nd question.
Regarding variables created by forms and stored in sessions.
Lets say page1.php contains a form, which is posted to page2.php.
page2.php session_registers the input so it can be used in later pages, it also displays some of the data.
The problem comes when the user gets to page2, clicks "Back" on browser, re-enters data, and re-submits. I see the old data in page2.
How do you avoid this, and get the new data?
Thanks,
Tony
Posted: Fri Apr 19, 2002 7:27 am
by Phirus
Page1:
Code: Select all
<form action="page2.php" method="post">
<input type="text" name="name" size="30" value="<?php echo $_SESSIONї'name']?>">
<input type="text" name="age" size="30" value="<?php echo $_SESSIONї'age']?>">
<input type="submit" name="submit" value="Continue">
</form>
Page2:
Code: Select all
<?php
session_start();
$_SESSIONї'name'] = $_POSTї'name'];
$_SESSIONї'age'] = $_POSTї'age'];
echo('<a href="page1.php">Go Back</a>');
?>
Hope that helps ya, I had a similar problem myself a few months back, I would also advise ya to take a
good look at
http://www.php.net/manual/en/ref.session.php
PhIrUs
Posted: Fri Apr 19, 2002 10:08 am
by timmy
What is it you're trying to do? I wouldn't use sessions to carry form variables accorss pages, I use $HTTP_POST|GET_VARS. Using sessions can create problems, as you experienced yourself. The session variable is already set, so if you go back to the previous page and continue the session, the registered variable is still there, unless you unset it. Here's what I do:
Code: Select all
if ($HTTP_POST_VARS)
{
while (list($lvar, $lvalue) = each($HTTP_POST_VARS))
{
echo "
<input type=hidden name="$lvar" value="$lvalue">
";
}
}
I put that under the form tag on each page I am using in the form process. That way, the form variables are still there, and they're not registered as a session variable, so they're automatically reset in the $HTTP_POST_VARS array.
hope that helps
Posted: Fri Apr 19, 2002 11:02 am
by BigE
timmy: I think what he's saying is that once he sets the session variables that they stay set like that even if the user re-submits the form. Sessions can be useful for using across the site but they can become a pain in the ass if you don't know how to use them. I myself preffer cookies

Posted: Thu May 23, 2002 7:18 pm
by Phirus
Its a lot handier if you do it right, What you have to do is change the request method, this will re-evaluate the $_SESSION vars
PhIrUs