简体   繁体   English

谷歌浏览器 cookie 的最长有效期

[英]Google Chrome maximum cookie expiry date

I was creating my website when I created a new cookie with this php line:当我使用此 php 行创建新 cookie 时,我正在创建我的网站:

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

I realised my browser (Google Chrome) refused to get that cookie.我意识到我的浏览器(谷歌浏览器)拒绝获取那个 cookie。 When I looked at my cookies in Google Chrome it wasn't there.当我在 Google Chrome 中查看我的 cookies 时,它不存在。 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?我的问题是,您可以为 Google chrome 中的 cookie 设置的最短过期时间是多少? Does anyone know of this policy?有人知道这个政策吗?

I have just tried that on a 64bit OS with Chrome as a browser and Apache as a server, and it works flawlessly. 我刚刚尝试在64位操作系统上使用Chrome浏览器和Apache作为服务器,它运行完美。 It shows the cookie's expiration time to be somewhere in the year 2113. 它显示cookie的到期时间是在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). dev-null-dweller可能是正确的: 2038年1月19日星期二 03:14:07 UTC之后的任何日期都会绕到1900年左右的某个时间,从而迫使cookie立即消失(在32位平台上,即)。

Work around this by setting cookie expiration times to be no more than 10 years in the future, or so. 通过将Cookie到期时间设置为将来不超过10年左右来解决此问题。 This is already beyond the reasonably expectable lifetime of any electronic device, which will hold it, anyways. 无论如何,这已经超出了任何电子设备的合理预期寿命。

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.如果有人试图理解为什么 Chrome 接受 cookie 但设置的最长过期日期比预期的要短,那么 Chrome 104 中有一个更改,它将最长过期时间设置为不超过 400 天。

Limiting expiration date of cookies to 400 days works for expires and max-age js settings as well.将 cookies 的到期日期限制为 400 天也适用于expiresmax-age js 设置。

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.我试图设置 3 年的 cookie 过期并得到奇怪的行为,就像一些 cookies 有效期到 2038 年一样,希望这个答案能节省一些时间。 BTW, for old cookies, set before v104, expiration date is not modified, at least for now.顺便说一句,对于旧的 cookies,在 v104 之前设置,至少现在没有修改到期日期。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM