Timothy Bassett
Timothy Bassett

Reputation: 129

Checking cookies on other pages

I am having trouble setting and checking cookies in PHP. What I would like to do is set a cookie on one page and on another page, check to see if that cookie exists.

On the first page, I set the cookie using:

setcookie ("conversionChecker", "anything", time() - 3600);

and on the 2nd page, I check if that cookie is set using:

if (isset($_COOKIE['conversionChecker'])){
  // include conversion code
}else{
  // dont include conversion code
}

However when I check for the cookie, it always returns false.

Upvotes: 1

Views: 88

Answers (2)

fanfavorite
fanfavorite

Reputation: 5199

The cookie is false because you are setting an expire time that has already passed. Try adding a plus instead of minus:

setcookie ("conversionChecker", "anything", time() + 3600);

time() is the current timestamp and 3600 is 1 hour. So it will expire one hour after it is set.

Upvotes: 3

CrayonViolent
CrayonViolent

Reputation: 32532

you are setting a cookie to a time in the past.. this is effectively the same as deleting a cookie.

Upvotes: 1

Related Questions