問題描述
查看 php 設(shè)置 cookie 文檔 我發(fā)現(xiàn)我可以設(shè)置 cookie 的過期日期.您可以將 cookie 設(shè)置為在瀏覽器會(huì)話結(jié)束時(shí)或?qū)砟硞€(gè)時(shí)間過期,但我沒有看到將 cookie 設(shè)置為永不過期的方法.這甚至可能嗎?這是如何實(shí)現(xiàn)的?
Looking at the php documentation on setting a cookie I 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 根據(jù) 過期cookie 規(guī)范,所以這不是 PHP 限制.
All cookies expire as per the cookie specification, so this is not a PHP limitation.
使用遙遠(yuǎn)的未來日期.比如設(shè)置一個(gè)十年后過期的cookie:
Use a far future date. For example, set a cookie that expires in ten years:
setcookie(
"CookieName",
"CookieValue",
time() + (10 * 365 * 24 * 60 * 60)
);
請(qǐng)注意,如果您在 32 位 PHP 中將日期設(shè)置為 2038,則該數(shù)字將環(huán)繞并且您將獲得一個(gè)立即過期的 cookie.
Note that if you set a date past 2038 in 32-bit PHP, the number will wrap around and you'll get a cookie that expires instantly.
這篇關(guān)于設(shè)置 cookie 永不過期的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!