gerkintrigg Posted August 28, 2012 Share Posted August 28, 2012 Hi all. I've got a simple logout script which used to work fine with sessions, bu as soon as I registered a cookie, everything started failing. I have tried this script on its own, but I just don't get it... Both the cookie and the session still says "Yes" - and that's not what's supposed to happen. Here's the code: session_start(); header("Cache-control: private"); //IE 6 Fix $root='../'; session_destroy(); session_unregister('Logged_In'); setcookie("Logged_In", 'No', time()-60); $go=$root.'logged_out.php'; echo 'Cookie: '.$_COOKIE['Logged_In']; echo '<br />Session: '.$_SESSION['Logged_In']; Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/ Share on other sites More sharing options...
darkfreaks Posted August 28, 2012 Share Posted August 28, 2012 i think this is what you are looking for Safely working with sessions & cookies Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/#findComment-1373118 Share on other sites More sharing options...
gerkintrigg Posted August 28, 2012 Author Share Posted August 28, 2012 Right... I'm not sure what I need to look at. It seems fine to me. But its not working. Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/#findComment-1373121 Share on other sites More sharing options...
darkfreaks Posted August 28, 2012 Share Posted August 28, 2012 when you do print_r($_COOKIE); what does it output? Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/#findComment-1373124 Share on other sites More sharing options...
gerkintrigg Posted August 28, 2012 Author Share Posted August 28, 2012 Array ( [Logged_In] => No [phpSESSID] => 553531b0c3b97a97a51bf7fab11ff507 [__utma] => 198886393.755584100.1324303205.1346139057.1346144284.339 [__utmb] => 198886393.92.9.1346146550090 [__utmc] => 198886393 [__utmz] => 198886393.1345877694.329.21.utmcsr=facebook.com|utmccn=(referral)|utmcmd=referral|utmcct=/l.php ) Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/#findComment-1373126 Share on other sites More sharing options...
darkfreaks Posted August 28, 2012 Share Posted August 28, 2012 change: setcookie("Logged_In", 'No', time()-60); to: setcookie("Logged_In", "", time()-60); Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/#findComment-1373131 Share on other sites More sharing options...
PFMaBiSmAd Posted August 28, 2012 Share Posted August 28, 2012 The $_COOKIE variables are set based on the cookies that the browser sends to the server with a http request. They won't show the result of a setcookie() statement until the next http request. Echoing a $_COOKIE variable right after a setcookie() statement doesn't show the current value in the cookie in the browser until the browser makes a http request to the server. session_unregister was depreciated 10 years ago and has been completely removed in php5.4. You should use unset to remove a single $_SESSION variable and if you want to clear all $_SESSION variables, use $_SESSION = array(); Quote Link to comment https://forums.phpfreaks.com/topic/267685-sessions-and-cookies-not-behaving/#findComment-1373152 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.