如何在 web.py 中删除/取消设置 cookie
在 web.py 中,您可以使用 web.webapi.cookies()
访问请求的 cookie,并且可以使用 web.webapi.setcookie(.. .)
。文档并不清楚如何删除 cookie,但是,您只是setcookie
并将值设置为 None 吗?
In web.py, you can get access to the request's cookies with web.webapi.cookies()
, and you can set the value of a cookie with web.webapi.setcookie(...)
. The documentation isn't clear on how one deletes a cookie, however -- do you just setcookie
with a value of None?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你是对的,从
setcookie()
的文档字符串或在线文档来看,这当然不明显,但它是 某处:例如,这是我们在注销代码中执行的部分操作(删除用户的会话 Cookie):
请注意,您必须删除具有相同域的 Cookie并确保您设置的安全标志,否则它不会删除。另外,对于 web.py,您通常使用
web.setcookie()
作为web.webapi.setcookie()
的快捷方式。You're right, it's certainly not obvious from
setcookie()
's docstring, or from the online docs, but it is there somewhere:For example, here's part of what we do in our sign-out code (delete the user's session cookie):
Note that you must delete the cookie with the same domain and secure flag as you set it with, otherwise it won't delete. Also, with web.py, you normally use
web.setcookie()
as a shortcut toweb.webapi.setcookie()
.web.py 似乎没有办法删除 cookie。如果您浏览 cookbook 中的文档,它很稀疏,甚至没有讨论诸如路径之类的内容(因此您可以将 cookie 设置到域内的特定位置)。因此,我们必须转向源代码。在此,我尽我所能,找不到任何对删除、删除或撤销 cookie 方法的引用。
话虽如此,经过测试,使用 None 来使 cookie 过期是安全的。这是一个可以显示它的快速网络应用程序。
在此,Web 应用程序首先检查 cookie 是否存在。如果没有,它将设置 cookie 'test1' 值为 'test'。下次刷新时,它将将该值更改为“test2”。下次刷新时,它会再次将该值设置为“test”,但也会使 cookie 过期。这应该会导致下一次刷新显示“我放火烧雨”。
web.py doesn't seem to have a way to delete a cookie. If you go through the documentation in the cookbook, it's sparse and doesn't even talk about things like path (so you can set a cookie to a specific location within a domain). So we must turn to the sourcecode. In this, try as I may, I cannot find any reference to a delete,remove, or revoke cookie method.
Having said that, after testing, it is safe to use None to expire a cookie. Here's a quick web app that'll display it.
In this the web app first checks if the cookie exists. If it doesn't it sets the cookie 'test1' with the value 'test'. On the next refresh, it will change the value to 'test2'. On the next refresh it sets the value again to 'test' but also expires the cookie. This should result in the next refresh showing 'I set fire to the rain'.