Form variables and sessions

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Tony Brush
Forum Newbie
Posts: 2
Joined: Fri Apr 19, 2002 6:18 am

Form variables and sessions

Post 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
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post by Phirus »

Page1:

Code: Select all

<form action="page2.php" method="post">
<input type="text" name="name" size="30" value="<?php echo $_SESSION&#1111;'name']?>">
<input type="text" name="age" size="30" value="<?php echo $_SESSION&#1111;'age']?>">
<input type="submit" name="submit" value="Continue">
</form>
Page2:

Code: Select all

<?php
session_start();
$_SESSION&#1111;'name'] = $_POST&#1111;'name'];
$_SESSION&#1111;'age'] = $_POST&#1111;'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
timmy
Forum Newbie
Posts: 15
Joined: Fri Apr 19, 2002 9:45 am
Location: Calgary, AB

Post 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) 
&#123;
	while (list($lvar, $lvalue) = each($HTTP_POST_VARS)) 
	&#123;
    echo "
<input type=hidden name="$lvar" value="$lvalue">
";
    &#125;
&#125;
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
User avatar
BigE
Site Admin
Posts: 139
Joined: Fri Apr 19, 2002 9:49 am
Location: Missouri, USA
Contact:

Post 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 :)
User avatar
Phirus
Forum Commoner
Posts: 37
Joined: Thu Apr 18, 2002 4:10 pm
Contact:

Post 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
Post Reply