Reputation: 83788
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?
Upvotes: 3
Views: 695
Reputation: 2999
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 the email.Utils
module in the Python standard library.
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 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.
Upvotes: 3
Reputation: 1345
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.
Upvotes: 2