我正在处理一个网站,想要在cookie中存储一个值。这个值是一个数字,在用户访问网站时,我想知道上次访问时的数字是什么,所以我考虑使用持久化cookie来存储当前值。当用户访问网站时,如果没有会话cookie,则会话cookie会复制持久性cookie。这样,会话cookie始终具有上次访问的值。
然而,尽管我将过期日期设置为从现在开始的一年,但似乎我的持久性cookie并没有被保留下来。
以下是我的Python代码:
然而,尽管我将过期日期设置为从现在开始的一年,但似乎我的持久性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)