HELP! Sessions won't work on more than 2 pages at a time!

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
ShadowIce
Forum Commoner
Posts: 75
Joined: Tue Jan 12, 2010 8:43 am

HELP! Sessions won't work on more than 2 pages at a time!

Post by ShadowIce »

How can I make this session code show the SAME results on sessionlesson02.php AND session03.php using the SAME session variables as defined in sessionlesson01.php? I need this as I plan to make a printable view page of a customer's receipt before they buy something, but in order to do that, I need to be able to use the same session variable more than 1 time on more than 2 pages all at one time.

sessionlesson.php:

Code: Select all

<?php
require('sessionlesson01.php');
?>
<html>
<head><title>Session Lesson</title></head>
<body>
<form name="sesstest" id="sesstest" action="sessionlesson02.php" method="POST">
<center>
<tr>
<td>Color: </td>
<td><input type="text" name="color01" id="color01" size="30"></td>
<br>
<td>Number: </td>
<td><input type="text" name="number01" id="number01" size="30"></td>
</tr>
<br><br>
<input type="submit" value="Submit">
</center>
</form>
</body>
</html>
sessionlesson01.php:

Code: Select all

<?php
session_start();
 
$_SESSION['color'] = $_POST['color01'];
$_SESSION['number'] = $_POST['number01'];
 
?>
sessionlesson02.php:

Code: Select all

<?php
require('sessionlesson01.php');
?><html>
<head><title>Session Lesson</title></head>
<body>
<form name="sesstest2" id="sesstest2" action="sessionlesson03.php" method="POST">
<center>
<?php
echo "Favorite Color: ".$_SESSION['color']."<br>\n";
echo "Favorite Number: ".$_SESSION['number']."<br>\n";
?>
<br>
<input type="submit" value="Submit">
</center>
</form>
</body>
</html>
session03.php:

Code: Select all

<?php
require('sessionlesson01.php');
?><html>
<head><title>Session Lesson</title></head>
<body>
<form name="sesstest3" id="sesstest3">
<center>
<?php
echo "Favorite Color: ".$_SESSION['color']."<br>\n";
echo "Favorite Number: ".$_SESSION['number']."<br>\n";
?>
</center>
</form>
</body>
</html>
Thanks!

ShadowIce~
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Post by Jonah Bron »

Remove the require statements from session02.php and session03.php. The Session variable is a lingering variable. In the last two pages, you are reseting the definition, essentially erasing it.
Post Reply