JPark Posted March 26, 2009 Share Posted March 26, 2009 I am trying to figure out sessions with 3 very simple pages and cannot seem to get it to work. Page 1 has a form that sends 3 inputs to Page 2. Page 2 has session variables and a link to Page 3. Page 3 tries to read the form data (should fail) and the session data (should work). What am I missing??? I've tried several different things... Page 1: <?php session_start(); ?> <html> <head> <title>Sessions Test : Page 1</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> To test sessions... <ol> <li>Create a simple form</li> <li>Send it to the second page.</li> <ul> <li>Does it keep the form information? </li> </ul> <li>Return to the first page.</li> <ul> <li>Does it keep the session information?</li> <li>What about any variables that were not stored?</li> </ul> <li>Go to a third page.</li> <ul> <li>Does it keep the session information?</li> <li>What about any variables that were not stored?</li> </ul> </ol> <? echo $_SESSION['color']," color<br />"; ?> <form action="page2.php" name="sessiontest" method="post" > <p>Choose a color: <input name="color" type="text" size="12" maxlength="12"></p> <p>Choose a size: <input name="size" type="text" size="12" maxlength="12"></p> <p>Choose a shape: <input name="shape" type="text" size="12" maxlength="12"></p> <input name="Submit" type="submit" value="Submit"> </form> <p>Go to <a href="page2.php">Page 2</a></p> <p>Go to <a href="page3.php">Page 3</a></p> </body> </html> Page 2: <? session_start(); ?> <html> <head> <title>Sessions Test : Page 2</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <p>Page 2</p> <? if (isset($_SESSION['color'])) $color = $_SESSION['color']; else $color = "None found"; $shape = $_SESSION['shape']; $size = $_SESSION['size']; $color2 = $_POST['color']; $shape2 = $_POST['shape']; $size2 = $_POST['size']; echo "SessionColor = ".$color."<br />"; echo "SessionShape = ".$shape."<br />"; echo "SessionSize = ".$size."<br /><br />"; echo "FormColor = ".$color2."<br />"; echo "FormShape = ".$shape2."<br />"; echo "FormSize = ".$size2."<br />"; ?> <p>Go back to <a href="page1.php">Page 1</a></p> <p>Go to <a href="page3.php">Page 3</a></p> </body> </html> Page 3: <? session_start(); ?> <html> <head> <title>Sessions Test : Page 3</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"> </head> <body> <? if (isset($_SESSION['color'])) $color = $_SESSION['color']; else $color = "None found"; $shape = $_SESSION['shape']; $size = $_SESSION['size']; $color2 = $_POST['color']; $shape2 = $_POST['shape']; $size2 = $_POST['size']; echo "SessionColor = ".$color."<br />"; echo "SessionShape = ".$shape."<br />"; echo "SessionSize = ".$size."<br /><br />"; echo "FormColor = ".$color2."<br />"; echo "FormShape = ".$shape2."<br />"; echo "FormSize = ".$size2."<br />"; ?> <p>Go back to <a href="page1.php">Page 1</a></p> <p>Go back to <a href="page2.php">Page 2</a></p> </body> </html> Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/ Share on other sites More sharing options...
ober Posted March 26, 2009 Share Posted March 26, 2009 I don't see anything that says: $_SESSION['blah'] = 'blah'; You're never setting anything to the session. You might want to read through this: http://www.phpfreaks.com/tutorial/sessions-and-cookies-adding-state-to-a-stateless-protocol Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/#findComment-794634 Share on other sites More sharing options...
revraz Posted March 26, 2009 Share Posted March 26, 2009 Also, use <?php tags in all your code instead of switching to short tags, makes it more portable. Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/#findComment-794638 Share on other sites More sharing options...
JPark Posted March 27, 2009 Author Share Posted March 27, 2009 I don't see anything that says: $_SESSION['blah'] = 'blah'; True. And that makes sense. But, how to you capture some output from a form and put it in a session variable? Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/#findComment-794819 Share on other sites More sharing options...
chmpdog Posted March 27, 2009 Share Posted March 27, 2009 use: $_SESSION['blah'] = $myvar; and on the next page.. if you want to use myvar then: $myvarfromlastpage = $_SESSION['blah']; Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/#findComment-794827 Share on other sites More sharing options...
JPark Posted March 27, 2009 Author Share Posted March 27, 2009 Rats! You beat me... I went to http://www.smallbizonline.co.uk/php_session_variables.php and figured it out: session_register ("color"); // Create a session variable called color session_register ("size"); // Create a session variable called size session_register ("shape"); // Create a session variable called shape $HTTP_SESSION_VARS ["color"] = $color; // Set name = form variable $color $HTTP_SESSION_VARS ["size"] = $size; // Set job = form variable $size $HTTP_SESSION_VARS ["shape"] = $shape; // Set job = form variable $shape $color2=$_POST['color']; // Grab the form variable called color $size2=$_POST['size']; // Grab the form variable called size $shape2=$_POST['shape']; // Grab the form variable called shape echo "The session variable color is ".$color."<br />"; echo "The form variable color is ".$color2."<br /><br />"; echo "The session variable size is ".$size."<br />"; echo "The form variable size is ".$size2."<br /><br />"; echo "The session variable shape is ".$shape."<br />"; echo "The form variable shape is ".$shape2."<br /><br />"; Is this the easiest/best way to do it? Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/#findComment-794832 Share on other sites More sharing options...
chmpdog Posted March 27, 2009 Share Posted March 27, 2009 the use of session_register() is deprecated. Therefore, i would still recommend the way I showed earlier... Link to comment https://forums.phpfreaks.com/topic/151271-solved-cant-figure-out-sessions/#findComment-794839 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.