Garethp Posted May 5, 2010 Share Posted May 5, 2010 I have the following code if(!isset($_COOKIE['TOS'])) { $expire = time() + 60*60*24*31*12*5; setcookie('TOS', 'No', $expire); } $TOS = $_COOKIE['TOS']; And on successful login, I have $expire = time() + 60*60*24*31*5; setcookie('TOS', 'Yes', $expire); Now, $TOS determines whether or not the Checkbox for "I accept the TOS" is checked on page load or not. I can login and logout and the checkbox will still be ticked. I can login, logout and close the tab and the checkbox will still be ticked. But once I close the browser, when I open it again the checkbox is unticked. Any idea what can be wrong? Quote Link to comment https://forums.phpfreaks.com/topic/200804-cookies-disappear-when-i-close-the-browser/ Share on other sites More sharing options...
Garethp Posted May 5, 2010 Author Share Posted May 5, 2010 Anybody? I don't mean to be a nag, but this is really bugging me Quote Link to comment https://forums.phpfreaks.com/topic/200804-cookies-disappear-when-i-close-the-browser/#findComment-1053677 Share on other sites More sharing options...
Muddy_Funster Posted May 5, 2010 Share Posted May 5, 2010 Pass, the only thing I can suggest is setcookie('TOS', 'Yes', $expire, 1); but I'm not hopefull. Quote Link to comment https://forums.phpfreaks.com/topic/200804-cookies-disappear-when-i-close-the-browser/#findComment-1053683 Share on other sites More sharing options...
PFMaBiSmAd Posted May 5, 2010 Share Posted May 5, 2010 Does closing the browser and visiting the page again also include using a different URL from the URL that was used when the cookie was set, such as one that has a www. on it or no-www. on it or is using a different path to the file? You are not specifying the path or domain parameters in the setcookie() so the cookie will only match the exact hostname/subdomain (www. is a hostname/subdomain) and path where the cookie was set at. Quote Link to comment https://forums.phpfreaks.com/topic/200804-cookies-disappear-when-i-close-the-browser/#findComment-1053686 Share on other sites More sharing options...
Garethp Posted May 5, 2010 Author Share Posted May 5, 2010 Turns out that I'd forgotten to set $expire outside of the if(!isset($_COOKIE['TOS'])), so when it set Yes, $expire was undefined, hence defaulting to 0. Thanks for the help anyway Quote Link to comment https://forums.phpfreaks.com/topic/200804-cookies-disappear-when-i-close-the-browser/#findComment-1053701 Share on other sites More sharing options...
PFMaBiSmAd Posted May 6, 2010 Share Posted May 6, 2010 We only see the information you supply in your posts. If you post code that shows you are setting a value right before you use it, we must assume that is your actual code or you would not have posted it together - $expire = time() + 60*60*24*31*5; setcookie('TOS', 'Yes', $expire); Quote Link to comment https://forums.phpfreaks.com/topic/200804-cookies-disappear-when-i-close-the-browser/#findComment-1054010 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.