在 Plone 中设置过期的 cookie
我想设置一个在未来几个小时内过期的 cookie
已经存在一个显示如何设置 cookie 的问题:
如何在 Zope 和 Plone 中获取和设置 cookie?
...但我没有找到如何生成 RFC 的示例第822章Zope 以“正确的方式”。看起来其他框架从日期时间内部生成时间戳。
另外,是否有可能让 cookie 在浏览器关闭时过期?这个是没有保质期的吗?
I'd like to set a cookie with expire time in few hours in the future
There already exist a question which shows how to set a cookie:
How do you get and set cookies in Zope and Plone?
... but I didn't find examples how to generate RFC 822 timestamp with Zope in "right way". Looks like other frameworks do timestamp generation internally from datetime.
Also is it possible to have cookies which expiry on a browser closing? Is this one without expiry date?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过在 cookie 上设置 expires 属性,将 cookie 设置为在将来的某个日期过期。这应该是使用 Python 标准库中的
email.Utils
模块中的formatdate
生成的 RFC822 值。(Internet Explorer 不支持 cookie 规范建议的 max-age 属性。)
如果您希望 cookie 在关闭浏览器时被清除,则无需设置过期值。
笔记。务必设置您的 cookie 有效的路径,否则它仅在您设置的页面上有效。
You can set a cookie to expire at a date in the future by setting an expires attribute on the cookie. This should be an RFC822 value generated with
formatdate
from theemail.Utils
module in the Python standard library.(Internet Explorer does not support the max-age attribute suggested by the cookie spec.)
Simply don't set an expires value if you want a cookie that is cleared when you close your browser.
Note. It's important to always set a path that your cookie is valid or it will only be valid on the page you set it.
您可以查看这两个问题的答案,以便了解如何生成有效的 RFC 822 日期时间 值。
为了创建一个在浏览器关闭后立即过期的 cookie,只需创建一个没有过期日期的 cookie。这将生成一个会话 cookie,该 cookie 将在浏览器会话过期后立即过期。
You can see the answers to these two questions, in order to understand how to generate valid RFC 822 date time value.
In order to create a cookie which expires as soon as the browser is closed, just create a cookie without a expiry date. This will generate a session cookie, which will expire as soon as the browser session expires.