2012-02-22 45 views
3

过期饼干我想设定到期在几个小时的时间在未来设置在Plone

已经存在一个问题,它展示了如何设置一个cookie饼干:

How do you get and set cookies in Zope and Plone?

...但我没有找到示例如何用“正确的方式”与Zope生成RFC 822时间戳。看起来像其他框架从日期时间内部时间戳生成。

也有可能有浏览器关闭到期的cookies?这是一个没有失效日期吗?

回答

2

您可以看到这两个问题的答案,以了解如何生成有效的RFC 822 date time值。

  1. What is the best way to convert a zope DateTime object into Python datetime object?
  2. How do I convert RFC822 to a python datetime object?

为了营造其尽快关闭浏览器过期饼干,只需创建一个cookie没有到期日。这将生成一个会话cookie,该会话cookie将在浏览器会话过期后立即失效。

+0

更安全地使用'email.Utils.formatdate(秒,usegmt = True)'推出自己的RFC 822格式化例程。 – 2012-02-22 21:48:32

3

您可以通过在cookie上设置expires属性来将cookie设置为将来某个日期到期。这应该是Python标准库中email.Utils模块的formatdate生成的RFC822值。

import time 
from email.Utils import formatdate 
expiration_seconds = time.time() + (5*60*60) # 5 hours from now 
expires = formatdate(expiration_seconds, usegmt=True) 
response.setCookie('cookie_name', 'value', path='/', expires=expires) 

(Internet Explorer不支持通过cookie规范建议的最大年龄属性。)

根本就没有设置,如果你想被清除cookie中,当你关闭浏览器的到期值。

注意。始终设置您的Cookie有效的路径非常重要,否则它只会在您设置的页面上有效。