Jump to content

Cookies Disappear when I close the browser


Garethp

Recommended Posts

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?

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.

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  :psychic:  -

 

$expire = time() + 60*60*24*31*5;
setcookie('TOS', 'Yes', $expire);

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.