创建一个持续,直到关闭浏览器的cookie(会话Cookie?) [英] Create a cookie that lasts until the browser is closed(Session Cookie?)

查看:219
本文介绍了创建一个持续,直到关闭浏览器的cookie(会话Cookie?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我熟悉分配,使用Python创建的cookie。但我不确定如何创建一个Cookie,将持续到当前的浏览器会话关闭(所以我必须告诉当用户的返回,以我的网站的一个非常基本的方式)。

因此​​,这饼干头做我设置,以确保cookie将到期/当浏览器在Python被关闭删除吗?难道我用SimpleCookie对象或其他对象的吗?

此线程说,我设置PHP的Python的cookie_lifetime标志/头,但怎么样? <一href=\"http://bytes.com/topic/php/answers/595383-how-declare-cookie-will-destroy-after-browser-closed\" rel=\"nofollow\">http://bytes.com/topic/php/answers/595383-how-declare-cookie-will-destroy-after-browser-closed

这会创建一个在浏览器的收盘到期的cookie?

 饼干= Cookie.SimpleCookie()
饼干[测试] =MYTEST
饼干[测试] ['过期'] = 0#或SHD我设置的最大年龄的头呢?
打印STR(饼干)+;仅Http


解决方案

刚刚离开了过期的价值完全,即不要将其设置为任何事情。详情请参阅维基百科条目


  

设置Cookie:made_write_conn = 1295214458;路径= /;域= .foo.com


  
  

[...]


  
  

第二饼干made_write_conn没有到期日,使其成为一个会话cookie。用户关闭他/她的浏览器之后,它将会被删除。


在Python的:

 在[11]:从饼干进口SimpleCookie[12]:C = SimpleCookie()在[13]:C ['测试'] ='MYTEST在[14]:打印Ç
设置Cookie:测试= MYTEST

I am familiar with assigning, creating cookies in Python. But I am unsure how to create a cookie that will last until the current browser session is closed(so I have a very rudimentary way of telling when the user is returning to my website).

So which cookie header do I set to make sure the cookie will expire/delete when the browser is closed in Python? Do I use the SimpleCookie object or another object for this?

This thread says I set the cookie_lifetime flag/header in PHP, but what about for python? http://bytes.com/topic/php/answers/595383-how-declare-cookie-will-destroy-after-browser-closed

Would this create a cookie that expires on closing of the browser?

cookie  = Cookie.SimpleCookie()
cookie["test"] = "MYTEST"
cookie["test"]['expires'] = 0 # or shd I set the max-age header instead?
print str(cookie) + "; httponly"

解决方案

Just leave out the "expires" value altogether, i.e. don't set it to anything. See the Wikipedia entry for details:

Set-Cookie: made_write_conn=1295214458; path=/; domain=.foo.com

[...]

The second cookie made_write_conn does not have expiration date, making it a session cookie. It will be deleted after the user closes his/her browser

In Python:

In [11]: from Cookie import SimpleCookie

In [12]: c = SimpleCookie()

In [13]: c['test'] = 'MYTEST'

In [14]: print c
Set-Cookie: test=MYTEST

这篇关于创建一个持续,直到关闭浏览器的cookie(会话Cookie?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆