如何在 web.py 中删除/取消设置 cookie

发布于 2024-10-28 04:56:52 字数 188 浏览 1 评论 0原文

在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-11-04 04:56:52

你是对的,从 setcookie() 的文档字符串或在线文档来看,这当然不明显,但它是 某处

web.setcookie() 的第三个(可选)参数“expires”允许您设置 cookie 何时过期。 任何负数都会使 Cookie 立即失效。

例如,这是我们在注销代码中执行的部分操作(删除用户的会话 Cookie):

    web.setcookie('session', '', expires=-1, domain=session_cookie_domain)

请注意,您必须删除具有相同域的 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:

The third (and optional) argument to web.setcookie(), "expires", allows you to set when you want your cookie to expire. Any negative number will expire the cookie immediately.

For example, here's part of what we do in our sign-out code (delete the user's session cookie):

    web.setcookie('session', '', expires=-1, domain=session_cookie_domain)

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 to web.webapi.setcookie().

农村范ル 2024-11-04 04:56:52

web.py 似乎没有办法删除 cookie。如果您浏览 cookbook 中的文档,它很稀疏,甚至没有讨论诸如路径之类的内容(因此您可以将 cookie 设置到域内的特定位置)。因此,我们必须转向源代码。在此,我尽我所能,找不到任何对删除、删除或撤销 cookie 方法的引用。

话虽如此,经过测试,使用 None 来使 cookie 过期是安全的。这是一个可以显示它的快速网络应用程序。

import web

web.config.debug = False
urls = (
        '/', 'index'
        )

class index:
    def GET(self):
        c = web.cookies().get('test1')
        if c:
            if c=="test":
                print 'this is ' + c
                web.setcookie('test1', 'test2', domain = 'example.com')
            else:
                print c
                web.setcookie('test1', 'test', domain = 'example.com' expires = None)
            return c
        else:
            print "didnt find cookie"       
            web.setcookie('test1', 'test', domain = 'example.com' expires='')
            return 'I set fire to the rain'

app = web.application(urls, globals())

if __name__ == "__main__":
    app.run()

在此,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.

import web

web.config.debug = False
urls = (
        '/', 'index'
        )

class index:
    def GET(self):
        c = web.cookies().get('test1')
        if c:
            if c=="test":
                print 'this is ' + c
                web.setcookie('test1', 'test2', domain = 'example.com')
            else:
                print c
                web.setcookie('test1', 'test', domain = 'example.com' expires = None)
            return c
        else:
            print "didnt find cookie"       
            web.setcookie('test1', 'test', domain = 'example.com' expires='')
            return 'I set fire to the rain'

app = web.application(urls, globals())

if __name__ == "__main__":
    app.run()

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'.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文