OldWest Posted October 20, 2010 Share Posted October 20, 2010 Hello, I've been hacking at this for about 5 hours, and have been unable to get my session data to pass through to the 3rd page. Here is my code. As you can see I've been trying all types of variation of retrieval. On page 2 I can pull the data from page 1 fine with REQUEST or the longer version.. But for some reason page 3 will not display the first_name and last_name... I'm desperate at this point. I have not been able to find a working answer. I'm running php 5 w/ trans... OFF (default) in .ini. Page 1 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; "<br /><br />"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>0</title> </head> <body> <form method="post" action="multi_page_post_2.php"> <label>First Name</label> <input name="first_name" value="" width="50" type="text" /> <br /><br /> <label>Last Name</label> <input name="last_name" value="" width="50" type="text" /> <br /><br /> <input type="submit" name="submit" value="Enter your Data - Continue to Step 2 >>" /> </form> </body> </html> Page 2 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; $first_name = $_REQUEST['first_name']; echo $first_name; ?> <?php echo "<br /><br />"; print_r($_SESSION); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Step 2</title> </head> <body> <h2>Step 2 - Is this data correct?</h2> <hr /> <strong>First Name:</strong> <?php echo $_SESSION['first_name'] = $_POST['first_name']; ?> <br /><br /> <strong>Last Name:</strong> <?php echo $_SESSION['last_name'] = $_POST['last_name']; ?> <br /><br /> <form method="post" action="multi_page_post_3.php"> <label>Sons First Name: </label> <input name="sons_first_name" value="" width="50" type="text" /> <br /><br /> <input type="submit" name="submit" value="YES! Continue to Step 3 >>" /> <input type="hidden" name="PHPSESSID" value="<?php echo $PHPSESSID ?>" /> <input type="button" value="No! Back to Previous Page" onClick="javascript: history.go(-1)"> </form> <br /><br /> <a href="" onclick="<?php session_destroy(); ?>">Session D E S T R O Y !</a> </body> </html> Page 3 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Step 3</title> </head> <body> <h2>Step 3 - Is this data correct?</h2> <hr /> <strong>First Name:</strong> <?php echo $_SESSION['first_name'] = $_REQUEST['first_name']; ?> <br /><br /> <strong>Last Name:</strong> <?php echo $_SESSION['last_name']; ?> <br /><br /> <strong>Sons First Name:</strong> <?php echo $_REQUEST['sons_first_name']; ?> <br /><br /> <form method="post" action="multi_page_post_4.php"> <input type="hidden" name="PHPSESSID" value="<?php echo $PHPSESSID ?>" /> </form> <br /><br /> <a href="" onclick="<?php session_destroy(); ?>">Session D E S T R O Y !</a> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/ Share on other sites More sharing options...
ram4nd Posted October 20, 2010 Share Posted October 20, 2010 cause you destroy your session on the third page Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124285 Share on other sites More sharing options...
Pikachu2000 Posted October 20, 2010 Share Posted October 20, 2010 What are you trying to do with these? Assign a value or echo a value? <strong>First Name:</strong> <?php echo $_SESSION['first_name'] = $_POST['first_name']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124403 Share on other sites More sharing options...
OldWest Posted October 20, 2010 Author Share Posted October 20, 2010 What are you trying to do with these? Assign a value or echo a value? <strong>First Name:</strong> <?php echo $_SESSION['first_name'] = $_POST['first_name']; ?> Hi Pikachu2000, I am trying to echo the data entered on the first page, have it show on the second page, added a new field for entry on the 2nd page, and ALL data be echoed on the 3rd page. What you see in my code is mixed and matched chunks of code trying to resolve this issue.. I don't think my session would be destroyed on the 3rd page as that requires an "onclick" as the previous poster stated. Thanks for any tips on this. I'm still trying to resolve it! Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124487 Share on other sites More sharing options...
PFMaBiSmAd Posted October 20, 2010 Share Posted October 20, 2010 Php code is executed when the page is requested. onclick="<?php session_destroy(); ?>" is meaningless because the php code was executed when the page was requested and output by the server. There is nothing in the javascript onclick event code (take a look at the 'view source' of the page in your browser.) Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124489 Share on other sites More sharing options...
OldWest Posted October 20, 2010 Author Share Posted October 20, 2010 Hello. I want to say thanks for the help with this session problem I was having. I was able to get it all working, and I was hoping anyone could look it over and critique my code to let me know if it should be improved or changed for better performance or just plain better coding practice. Thanks in advance. Page 1 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; "<br /><br />"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>0</title> </head> <body> <form method="post" action="multi_page_post_2.php"> <label>First Name</label> <input name="first_name" value="" width="50" type="text" /> <br /><br /> <label>Last Name</label> <input name="last_name" value="" width="50" type="text" /> <br /><br /> <input type="submit" name="submit" value="Enter your Data - Continue to Step 2 >>" /> </form> </body> </html> Page 2 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; ?> <?php echo "<br /><br />"; print_r($_SESSION); ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Step 2</title> </head> <body> <h2>Step 2 - Is this data correct?</h2> <hr /> <strong>First Name:</strong> <?php echo $_SESSION['first_name'] = $_REQUEST['first_name']; ?> <br /><br /> <strong>Last Name:</strong> <?php echo $_SESSION['last_name'] = $_REQUEST['last_name']; ?> <br /><br /> <form method="post" action="multi_page_post_3.php"> <label>Sons First Name: </label> <input name="sons_first_name" value="" width="50" type="text" /> <br /><br /> <input type="submit" name="submit" value="YES! Continue to Step 3 >>" /> <input type="hidden" name="PHPSESSID" value="<?php echo $PHPSESSID ?>" /> <input type="button" value="No! Back to Previous Page" onClick="javascript: history.go(-1)"> </form> </body> </html> Page 3 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Step 3</title> </head> <body> <h2>Step 3 - Is this data correct?</h2> <hr /> <strong>First Name:</strong> <?php echo $_SESSION['first_name']; ?> <br /><br /> <strong>Last Name:</strong> <?php echo $_SESSION['last_name']; ?> <br /><br /> <strong>Sons First Name:</strong> <?php echo $_REQUEST['sons_first_name']; ?> <br /><br /> <form method="post" action="multi_page_post_4.php"> <input type="submit" name="submit" value="Destroy the Session! - Step 4 >>" /> <input type="hidden" name="PHPSESSID" value="<?php echo $PHPSESSID ?>" /> </form> <br /><br /> <a href="" onclick="<?php //session_destroy(); ?>">Session D E S T R O Y !</a> </body> </html> Page 4 <?php session_start(); ?> <?php $PHPSESSID = session_id(); echo "<strong>Session ID:</strong> $PHPSESSID"; ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Step 4</title> </head> <body> <h2>Step 4 - Session Evaporation</h2> <?php if ($_SESSION[''] = 1) { session_destroy(); echo "Toast!"; } else { echo "Not Toast!"; }; ?> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124555 Share on other sites More sharing options...
phpfreak Posted October 20, 2010 Share Posted October 20, 2010 Some things confuse me here, and concern me... <input type="hidden" name="PHPSESSID" value="<?php echo $PHPSESSID ?>" /> Why do you post the SESSION ID in a form? This should be saved on the server / user's cookie. If you put this in a form, you could potentially allow someone to override it.. but that would have to be some intelligent work - but possible. Possibly a security risk? <?php if ($_SESSION[''] = 1) { session_destroy(); echo "Toast!"; } else { echo "Not Toast!"; }; ?> You're defining $_SESSION[''] (nothing) as 1 here - not testing it. Use the '==' operator instead. However, this may be more practical: <?php if (session_id() > 0) { session_destroy(); echo "Toast!"; } else { echo "Not Toast!"; }; ?> I'd check up on these functions here and see how they are used in more detail: http://www.php.net/manual/en/function.session-id.php Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124578 Share on other sites More sharing options...
OldWest Posted October 20, 2010 Author Share Posted October 20, 2010 Some things confuse me here, and concern me... <input type="hidden" name="PHPSESSID" value="<?php echo $PHPSESSID ?>" /> Why do you post the SESSION ID in a form? This should be saved on the server / user's cookie. If you put this in a form, you could potentially allow someone to override it.. but that would have to be some intelligent work - but possible. Possibly a security risk? Hi phpfreak, I should have probably been more clear in my original post I was simply trying to resolve the issue of a user's Cookies being OFF or disabled in the browser. And scenario of trans-id also being OFF (as you know is default in php5). Now. I'm not totally sure if my concern is of proper merit. But it seems like Cookies being OFF is highly unlikely for most browser users. It's possible my approach makes zero sense. So please feel free to share any thoughts. Mainly diving into this area because I want to understand sessions more and how I can use them more effectively in my day to day tasks. Thanks. <?php if ($_SESSION[''] = 1) { session_destroy(); echo "Toast!"; } else { echo "Not Toast!"; }; ?> You're defining $_SESSION[''] (nothing) as 1 here - not testing it. Use the '==' operator instead. However, this may be more practical: <?php if (session_id() > 0) { session_destroy(); echo "Toast!"; } else { echo "Not Toast!"; }; ?> I'd check up on these functions here and see how they are used in more detail: http://www.php.net/manual/en/function.session-id.php Quote Link to comment https://forums.phpfreaks.com/topic/216341-session-data-is-not-being-carried-to-3rd-page/#findComment-1124602 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.