php 过期时间为“会话结束时”的 cookie 何时过期?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4132095/
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
When does a cookie with expiration time 'At end of session' expire?
提问by Shakti Singh
There is a session cookie with expiration time which says 'At end of session'. When exactly does it expire or will it be alive forever?
有一个带有过期时间的会话 cookie,上面写着“在会话结束时”。它到底什么时候到期,还是会永远活着?
采纳答案by mingos
When you use setcookie
, you can either set the expiration time to 0
or simply omit the parametre - the cookie will then expire at the end of session (ie, when you close the browser).
当您使用 时setcookie
,您可以将过期时间设置为0
或简单地省略参数 - cookie 将在会话结束时过期(即,当您关闭浏览器时)。
回答by James
Cookies that 'expire at end of the session' expire unpredictably from the user's perspective!
从用户的角度来看,“在会话结束时过期”的 Cookie会不可预测地过期!
On iOS with Safari they expire whenever you switch apps!
在带有 Safari 的 iOS 上,只要您切换应用程序,它们就会过期!
On Android with Chrome they don'texpire when you close the browser.
在带有 Chrome 的 Android 上,它们不会在您关闭浏览器时过期。
On Windows desktop running Chrome they expire when you close the browser. That's not when you close your website's tab; its when you close all tabs. Nor do they expire if there are any other browser windows open. If users run web apps as windows they might not even know they are browser windows. So your cookie's life depends on what the user is doing with some apparently unrelated app.
在运行 Chrome 的 Windows 桌面上,它们会在您关闭浏览器时过期。那不是当您关闭网站的选项卡时;当您关闭所有选项卡时。如果有任何其他浏览器窗口打开,它们也不会过期。如果用户将 Web 应用程序作为窗口运行,他们甚至可能不知道它们是浏览器窗口。因此,您的 cookie 的生命周期取决于用户正在使用一些明显无关的应用程序做什么。
回答by J.Smith
Just to correct mingos' answer:
只是为了纠正 mingos 的回答:
If you set the expiration time to 0, the cookie won't be created at all. I've tested this on Google Chrome at least, and when set to 0 that was the result. The cookie, I guess, expires immediately after creation.
如果您将过期时间设置为 0,则根本不会创建 cookie。我至少在谷歌浏览器上测试过这个,当设置为 0 时就是结果。我猜 cookie 会在创建后立即过期。
To set a cookie so it expires at the end of the browsing session, simply OMIT the expiration parameter altogether.
要设置 cookie 使其在浏览会话结束时过期,只需完全省略过期参数。
Example:
例子:
Instead of:
代替:
document.cookie = "cookie_name=cookie_value; 0; path=/";
Just write:
写就好了:
document.cookie = "cookie_name=cookie_value; path=/";
回答by shamittomar
End of the user session means when the browser is shut down.
用户会话结束意味着浏览器关闭。
Read this: http://en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age
阅读:http: //en.wikipedia.org/wiki/HTTP_cookie#Expires_and_Max-Age