8

I was creating my website when I created a new cookie with this php line :

setcookie('subscribed', 'true', time() + 365*24*3600*100, '/', null, false, true);

I realised my browser (Google Chrome) refused to get that cookie. When I looked at my cookies in Google Chrome it wasn't there. I started fiddling with the different settings until I saw that this worked :

setcookie('subscribed', 'true', time() + 365*24*360, '/', null, false, true);

Which meant that changing the expiration time to a lower value did work as a means of making this work.

My question is, what is the lowest expiration time you can set for a cookie in Google chrome? Does anyone know of this policy?

Farid
  • 215
  • 1
  • 2
  • 8

2 Answers2

12

I have just tried that on a 64bit OS with Chrome as a browser and Apache as a server, and it works flawlessly. It shows the cookie's expiration time to be somewhere in the year 2113.

dev-null-dweller is probably right: Any date beyond 03:14:07 UTC on Tuesday, 19 January 2038 will wrap around to some time close to 1900, thus forcing the cookie to immediately disappear (on 32bit platforms, that is).

Work around this by setting cookie expiration times to be no more than 10 years in the future, or so. This is already beyond the reasonably expectable lifetime of any electronic device, which will hold it, anyways.

likeitlikeit
  • 5,563
  • 5
  • 42
  • 56
  • Yep, I am in a 32bit system, and yep, setting the time to 10 years did work. Thanks a lot! – Farid May 18 '13 at 17:36
7

If someone is trying to understand why Chrome accepts cookie but sets max expire date to be shorter than expected, there was a change in Chrome 104 which sets max expire time to be no more than 400 days.

Limiting expiration date of cookies to 400 days works for expires and max-age js settings as well.

Assume other browsers will follow soon.

I was trying to set 3 years cookie expiration and got strange behavior, as had some cookies valid until 2038, hope this answer will save some time. BTW, for old cookies, set before v104, expiration date is not modified, at least for now.

Sergiy T.
  • 1,433
  • 1
  • 23
  • 25
  • 2
    This should be the new approved answer, as there is no way to go above that 400 days limit. – Will59 Sep 30 '22 at 12:35