Roman
Roman

Reputation: 10403

Why is the cookie expiring immediately?

In my code (testing on my local machine with domain localdev.xxx.com) I use the following code to set a cookie in browser which will expire in 6 months time:

$cookieExpiresOn = time() + (60 * 60 * 24 * 184);
setcookie("ref", "somevalue", $cookieExpiresOn);

however when I check the http response using firebug the set-cookie header has the date set to 1st of January 1970.

Set-Cookie: ref=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT;

Isn't time() function supposed to return the current date and time since Unix Epoch time? Why is this happening?

Upvotes: 3

Views: 4619

Answers (1)

Roman
Roman

Reputation: 10403

Found what the problem was. In my actual code the value was being set through a variable like so:

setcookie("ref", $varx, $cookieExpiresOn);

Apparently if cookie value is an empty string then php attempts to delete the cookie by setting its expire date to before current GMT time.

Upvotes: 7

Related Questions