stuckwithcode Posted March 9, 2010 Share Posted March 9, 2010 my page is called test.php, when i type in the address http://localhost/test.php?test=true why does the code not echo the word hello and then end the session. <?php session_start(); if ($_GET['test']) { $_SESSION['testsession'] = true; header('Location: test.php'); } ?> <!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> <title>Untitled Document</title> <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> </head> <body> <?php if (isset($_SESSION['testsession'])) { echo "Hello"; unset($_SESSION['testsession']); } ?> </body> </html> thanks if not possible other ideas welcome Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/ Share on other sites More sharing options...
isedeasy Posted March 9, 2010 Share Posted March 9, 2010 try if (isset($_GET['test']) && $_GET['test'] == 'true') { $_SESSION['testsession'] = true; header('Location: test.php'); } Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023689 Share on other sites More sharing options...
stuckwithcode Posted March 9, 2010 Author Share Posted March 9, 2010 no that still does not work Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023692 Share on other sites More sharing options...
PFMaBiSmAd Posted March 9, 2010 Share Posted March 9, 2010 Because you are redirecting to the same page, but without the ?test=true on the end of the URL, you are only 'seeing' the result of the page being requested the 2nd time. if ($_GET['test']) will be FALSE the 2nd time the page is requested, but because the code on the page continued executing the first time it was requested, $_SESSION['testsession'] was previously unset and the echo code is skipped over when the page is requested the 2nd time. I'm not sure what you are trying to accomplish with the posted code, but a header redirect normally needs an exit/die statement following it to prevent the remainder of the code on the page from being executed while the browser is performing the redirect. Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023693 Share on other sites More sharing options...
stuckwithcode Posted March 9, 2010 Author Share Posted March 9, 2010 doesn't the php redirect stop the html body being executed the first time round? Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023694 Share on other sites More sharing options...
PFMaBiSmAd Posted March 9, 2010 Share Posted March 9, 2010 php redirect There is no such thing. header('Location: test.php'); sends a header to the browser, telling it to request the test.php page. Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023697 Share on other sites More sharing options...
stuckwithcode Posted March 9, 2010 Author Share Posted March 9, 2010 So is my session being unset when the url is http://localhost/test.php?test=true and before the page is redirected, I thought the php in the body would not be executed because of the redirect. Thanks Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023698 Share on other sites More sharing options...
mikesta707 Posted March 9, 2010 Share Posted March 9, 2010 no, it simply sends another request to the page, not a redirect, but if you don't stop execution with die() or exit(), the rest of the page will be executed (at least until the header request is sent, and the page is reloaded adding a die() or exit() after the header should fix it Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023699 Share on other sites More sharing options...
stuckwithcode Posted March 9, 2010 Author Share Posted March 9, 2010 thanks mikesta707 problem solved Quote Link to comment https://forums.phpfreaks.com/topic/194647-in-need-of-help/#findComment-1023724 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.