php 设置 cookie 永不过期
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3290424/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set a cookie to never expire
提问by brainimus
Looking at the php documentation on setting a cookieI see that I can set an expiration date for the cookie. You can set the cookie to expire at the end of the browser session or at some time in the future but I do not see a way to set the cookie to never expire. Is this even possible and how is this accomplished?
查看有关设置 cookie的php 文档,我发现我可以为 cookie 设置到期日期。您可以将 cookie 设置为在浏览器会话结束时或将来某个时间过期,但我没有看到将 cookie 设置为永不过期的方法。这甚至是可能的,这是如何实现的?
回答by Joeri Hendrickx
All cookies expire as per the cookie specification, so this is not a PHP limitation.
所有 cookie 根据cookie 规范过期,因此这不是 PHP 限制。
Use a far future date. For example, set a cookie that expires in ten years:
使用遥远的未来日期。例如,设置一个十年后过期的cookie:
setcookie(
"CookieName",
"CookieValue",
time() + (10 * 365 * 24 * 60 * 60)
);
Note that if you set a date past 2038in 32-bit PHP, the number will wrap around and you'll get a cookie that expires instantly.
请注意,如果您在 32 位 PHP 中设置超过2038年的日期,则该数字将环绕并且您将获得一个立即过期的 cookie。
回答by PiTheNumber
Maximum value: 2147483647
最大值:2147483647
setcookie("CookieName", "CookieValue", 2147483647);
To avoid integer overflow the timestamp should be set to:
为避免整数溢出,时间戳应设置为:
2^31 - 1 = 2147483647 = 2038-01-19 04:14:07
Setting a higher value might cause problems with older browsers.
设置更高的值可能会导致旧浏览器出现问题。
Also see the RFC about cookies:
Max-Age=value OPTIONAL. The value of the Max-Age attribute is delta-seconds, the lifetime of the cookie in seconds, a decimal non-negative integer. To handle cached cookies correctly, a client SHOULD calculate the age of the cookie according to the age calculation rules in the HTTP/1.1 specification [RFC2616]. When the age is greater than delta-seconds seconds, the client SHOULD discard the cookie. A value of zero means the cookie SHOULD be discarded immediately.
Max-Age=value OPTIONAL. The value of the Max-Age attribute is delta-seconds, the lifetime of the cookie in seconds, a decimal non-negative integer. To handle cached cookies correctly, a client SHOULD calculate the age of the cookie according to the age calculation rules in the HTTP/1.1 specification [RFC2616]. When the age is greater than delta-seconds seconds, the client SHOULD discard the cookie. A value of zero means the cookie SHOULD be discarded immediately.
and RFC 2616, 14.6 Age:
If a cache receives a value larger than the largest positive integer it can represent, or if any of its age calculations overflows, it MUST transmit an Age header with a value of 2147483648 (2^31).
如果缓存接收到的值大于它可以表示的最大正整数,或者它的任何年龄计算溢出,它必须传输一个值为 2147483648 (2^31) 的 Age 标头。
回答by David
Set a far future absolute time:
设置一个遥远的未来绝对时间:
setcookie("CookieName", "CookieValue", 2147483647);
It is better to use an absolute time than calculating it relative to the present as recommended in the accepted answer.
最好使用绝对时间而不是按照已接受的答案中的建议相对于现在计算它。
The maximum value compatible with 32 bits systems is:
与 32 位系统兼容的最大值为:
2147483647 = 2^31 = ~year 2038
回答by John
My privilege prevents me making my comment on the first post so it will have to go here.
我的特权阻止我对第一篇文章发表评论,所以它必须在这里发表。
Consideration should be taken into account of 2038 unix bugwhen setting 20 years in advance from the current date which is suggest as the correct answer above.
从当前日期提前 20 年设置时,应考虑2038 unix 错误,建议作为上述正确答案。
Your cookie on January 19, 2018 + (20 years) could well hit 2038 problem depending on the browser and or versions you end up running on.
您在 2018 年 1 月 19 日 +(20 年)的 cookie 很可能会遇到 2038 问题,具体取决于您最终运行的浏览器和/或版本。
回答by Jesus
Can't you just say a never ending loop, cookie expires as current date + 1 so it never hits the date it's supposed to expire on because it's always tomorrow? A bit overkill but just saying.
你不能说一个永无止境的循环,cookie 作为当前日期 + 1 过期,所以它永远不会到达它应该过期的日期,因为它总是明天?有点矫枉过正,但只是说。
回答by h3r2on
While that isn't exactly possible you could do something similar to what Google does and set your cookie to expire Jan 17, 2038 or something equally far off.
虽然这不太可能,但您可以做一些类似于 Google 所做的事情,并将您的 cookie 设置为 2038 年 1 月 17 日或同样遥远的到期日。
In all practicality you might be better off setting your cookie for 10 years or 60*60*24*365*10, which should outlive most of the machines your cookie will live on.
实际上,您最好将 cookie 设置为 10 年或 60*60*24*365*10,这应该比您的 cookie 所在的大多数机器的寿命都长。
回答by Bj?rn
If you want to persist data on the client machine permanently -or at least until browser cache is emptied completely, use Javascript local storage:
如果您想在客户端机器上永久保留数据 - 或者至少直到浏览器缓存完全清空,请使用 Javascript 本地存储:
https://developer.mozilla.org/en-US/docs/DOM/Storage#localStorage
https://developer.mozilla.org/en-US/docs/DOM/Storage#localStorage
Do not use session storage, as it will be cleared just like a cookie with a maximum age of Zero.
不要使用会话存储,因为它会像最大年龄为零的 cookie 一样被清除。
回答by CONvid19
Neverand foreverare two words that I avoid using due to the unpredictability of life.
由于生活的不可预测性,从不和永远是我避免使用的两个词。
The latest time since 1 January 1970that can be stored using a signed 32-bitinteger is 03:14:07 on Tuesday, 19 January 2038(231-1 = 2,147,483,647seconds after 1 January 1970). This limitation is known as the Year 2038 problem
自1 January 1970那以后可以使用有符号32-bit整数存储的最晚时间是03:14:07 on Tuesday, 19 January 2038(231-1 = 2,147,483,647秒后1 January 1970)。此限制称为2038 年问题
setCookie("name", "value", strtotime("2038-01-19 03:14:07"));
回答by Sarfraz
You shouldn't do that and that's not possible anyway, If you want you can set a greater value such as 10 years ahead.
你不应该那样做,无论如何那是不可能的,如果你愿意,你可以设置一个更大的值,比如 10 年。
By the way, I have never seen a cookie with such requirement :)
顺便说一句,我从未见过有这种要求的 cookie :)
回答by Joel Kennedy
I believe that there isn't a way to make a cookie last forever, but you just need to set it to expire far into the future, such as the year 2100.
我相信没有办法让 cookie 永远持续下去,但您只需要将它设置为在很远的将来过期,例如 2100 年。

