python Bottle持久cookie不起作用
我正在开发一个网站,我想在 cookie 中存储一个值
,这是一个数字,当用户访问该网站时,我想知道他们上次访问时的数字是什么,所以我正在考虑存储当前值的持久 cookie,当用户访问站点时,如果没有会话 cookie,则会话 cookie 会获取持久 cookie 的副本。 这样,会话 cookie 始终具有上次访问的值。
即使我设置了 1 年后的到期日期,我的持久 cookie 似乎也没有被持久化,
这是我的 python 代码:
persistentCookieKey = category + '_highest_id'
sessionCookieKey = 'session_' + persistentCookieKey + '_highest_id'
persistentCookieValue = request.get_cookie(persistentCookieKey)
if persistentCookieValue == None:
persistentCookieValue = 0 # each time i restart my browser it comes through here!
sessionCookieValue = request.get_cookie(sessionCookieKey)
print 'persistentCookieValue:', persistentCookieValue
print 'sessionCookieValue:', sessionCookieValue
if sessionCookieValue == None:
print 'session cookie not set, setting to:', persistentCookieValue
sessionCookieValue = persistentCookieValue
response.set_cookie(sessionCookieKey, str(persistentCookieValue))
print 'setting persistent cookie to value:', highestId
expireDate = date.today() + timedelta(days=365)
response.set_cookie(persistentCookieKey, str(highestId), expires=expireDate)
highestIdLastVisit = int(sessionCookieValue)
I have a site im working on, i want to store a value in a cookie
this is an number, when the user comes to the website, i want to know what the number was on their last visit, so I'm thinking of having a persistant cookie that stores the current value, when the user comes to the site, if there is no session cookie, then the session cookie grabs a copy of the persistant cookie.
This way the session cookie always has the value from the last visit.
how ever it seems that my persistant cookie isnt being persisted even though i've set the expiry date 1 year from now
here is my python code:
persistentCookieKey = category + '_highest_id'
sessionCookieKey = 'session_' + persistentCookieKey + '_highest_id'
persistentCookieValue = request.get_cookie(persistentCookieKey)
if persistentCookieValue == None:
persistentCookieValue = 0 # each time i restart my browser it comes through here!
sessionCookieValue = request.get_cookie(sessionCookieKey)
print 'persistentCookieValue:', persistentCookieValue
print 'sessionCookieValue:', sessionCookieValue
if sessionCookieValue == None:
print 'session cookie not set, setting to:', persistentCookieValue
sessionCookieValue = persistentCookieValue
response.set_cookie(sessionCookieKey, str(persistentCookieValue))
print 'setting persistent cookie to value:', highestId
expireDate = date.today() + timedelta(days=365)
response.set_cookie(persistentCookieKey, str(highestId), expires=expireDate)
highestIdLastVisit = int(sessionCookieValue)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Bottle 使用 http://docs.python.org/library/cookie.html 来实现cookie 支持。此实现要求
expires
参数为Wdy, DD-Mon-YY HH:MM:SS GMT
格式的字符串。传递日期时间或日期对象会默默失败。我将在 Bottle 的未来版本中修复这个问题(嗨,我是作者),但现在我建议使用
max_age
代替。编辑:哦,我刚刚注意到它的记录也不正确。对此感到抱歉。
编辑2:已修复(在master中)
Bottle uses http://docs.python.org/library/cookie.html to implement cookie support. This implementation requires the
expires
parameter to be a string in theWdy, DD-Mon-YY HH:MM:SS GMT
format. Passing datetime or date objects fails silently.I'll fix that in future versions of Bottle (hi, I'm the author) but for now I suggest using
max_age
instead.Edit: Oh, and I just noticed it is also documented incorrectly. Sorry for that.
Edit2: Fixed (in master)