user2795877
user2795877

Reputation: 13

Cookie expiration is browser specific or server specific?

I am setting a a cookie with an expiration time by

mktime(24,0,0).

My question is simple. If browsers timezone is different , will cookie follow the server's timezone to expire or browser's timezone ?

Upvotes: 0

Views: 459

Answers (3)

user2795877
user2795877

Reputation: 13

mktime(24,0,0)

Calculates the unix timestamp of next day 00:00:00 based on the timezone() server machine you are running and returns the integer timestamp.

Now,when you pass that in the setcookie(Visit, date("F jS - g:i a"), mktime(24,0,0)); On the browser, it converts this 'timestamp since epoch' to the local time zone and sets the expiration for the cookie.

You should be knowing we are here still controlling the cookie lifespan based on the server side time frame.

Upvotes: 0

Josh J
Josh J

Reputation: 6893

The Set-Cookie header has timezone information as part of the expires datetime so the user agent knows when it should expire.

Set-Cookie: sessionToken=abc123; Expires=Wed, 09 Jun 2021 10:18:14 GMT

From the php docs for setcookie

expire

...

Note: You may notice the expire parameter takes on a Unix timestamp, as opposed to the date format Wdy, DD-Mon-YYYY HH:MM:SS GMT, this is because PHP does this conversion internally.

Upvotes: 1

Thomas Dalbert
Thomas Dalbert

Reputation: 58

From PHP manual, mktime function :

Returns the Unix timestamp corresponding to the arguments given. This timestamp is a long integer containing the number of seconds between the Unix Epoch (January 1 1970 00:00:00 GMT) and the time specified.

It contains a number of second, a quantity of time : there is no need to care about timezone.

Upvotes: 1

Related Questions