简体   繁体   English

一个cookie的多个过期选项? (JavaScript的)

[英]Multiple expire options for one cookie? (javascript)

I was wondering if there is a way to set multiple expire options for one cookie. 我想知道是否有办法为一个cookie设置多个过期选项。 For example, I want a cookie to expire when the user closes their browser and in 30 minutes. 例如,我希望cookie在用户关闭浏览器时到期并在30分钟内到期。

Is this possible? 这可能吗?

YAHOO.util.Cookie.setSubs("cookiename", cookieData, { expires: 0, expires: time() - 1800, path: "/", domain: "cbc.ca" }); YAHOO.util.Cookie.setSubs(“cookiename”,cookieData,{expires:0,expires:time() - 1800,path:“/”,domain:“cbc.ca”});

From the RFC: 来自RFC:

If an attribute appears more than once in a cookie, the behavior is undefined. 如果属性在cookie中出现多次,则行为未定义。

There's no way to set up a cookie to expire like that; 没有办法设置一个像这样过期的cookie; at least, there's no cross-browser way to do it. 至少,没有跨浏览器的方式来做到这一点。 (Also, as far as I know the appropriate attribute is "Max-Age", not "expires"; maybe that name is part of the YUI api however.) (另外,据我所知,适当的属性是“Max-Age”,而不是“expires”;也许这个名字是YUI api的一部分。)

Expiring a session after a period of time is generally something that secure server-side code does on its own, and explicitly. 在一段时间之后使会话过期通常是保护服务器端代码本身和显式安全的事情。 (In other words, the session cookie is explicitly rejected as invalid if its timestamp indicates excessive age.) (换句话说,如果会话cookie的时间戳超过年龄,则会明确拒绝会话cookie。)

You can have the cookie set the onload event, but you can not have two expire times. 您可以让cookie设置onload事件,但不能有两个到期时间。 You can get the time the page unloads and then set the cookie for +30 minutes. 您可以获取页面卸载的时间,然后将cookie设置为+30分钟。 But you Cant have the cookie set when the browser closes, you would need a plugin to do that, but when the page closes you can. 但是你不能在浏览器关闭时设置cookie,你需要一个插件才能这样做,但是当页面关闭时你可以。

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

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