php - What's the proper way to test for an unexpired cookie?

279

I'm having trouble getting my code to ignore loading an expired cookie.

When I log a user in who wants to be remembered, I set a cookie like so...

setcookie('mysiteCookieBlah', $token, $expiry, '/', 'https://www.mysite.com', 1, 1); 

Whenever they refresh the page, if they're not logged in ie don't have a user ID in session, I then check for the existence of the cookie to see if I should init auto-logging them in...

if(isset($_COOKIE['mysiteCookieBlah'])) 
  $intiAutoLogin=true; 
else 
  $intiAutoLogin=false;  

When they log out, I expire it...

unset($_COOKIE['mysiteCookieBlah']);
setcookie('mysiteCookieBlah', "", time()-3600, '/', 'https://www.mysite.com', 1, 1);

Next time the page refreshes,

if(isset($_COOKIE['mysiteCookieBlah'])) 
  $intiAutoLogin = ?

should = false, right? At the moment, it sees the cookie every time, regardless if it's expired.

Thanks for taking a look.

602

Answer

Solution:

I changed my logout cookie code to

setcookie("mysiteCookieBlah", "", time()-3600, '/');

and it worked. I have no idea why.

People are also looking for solutions to the problem: php - Save textFile dates into MySQL

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.