Deleting a cookie

429 views
Skip to first unread message

Dane

unread,
Feb 7, 2011, 2:28:48 AM2/7/11
to web2py-users, lprubin...@gmail.com
Hello,

I've got my own user system (not using Auth), and I'm trying to
implement a Remember Me option with cookies. It's working fine when
the user selects 'Yes' for remember me. Setting and detecting the
cookie is no problem, but I can't find a way to delete the cookie when
the user logs out or selects 'No'. Here's my code which attempts
everything I can think of.

if request.cookies.has_key('user'):
request.cookies['user'].clear()
del request.cookies['user']
print 'remove request user cookie'
if response.cookies.has_key('user'):
response.cookies['user'].clear()
del response.cookies['user']
print 'remove response user cookie'

After running this, request.cookies.has_key('user') is still returning
true and the cookie's data is still present.

Ideas? Thanks.

Massimo Di Pierro

unread,
Feb 7, 2011, 9:28:36 AM2/7/11
to web2py-users
http://msdn.microsoft.com/en-us/library/ms178195.aspx

"You cannot directly delete a cookie on a user's computer. However,
you can direct the user's browser to delete the cookie by setting the
cookie's expiration date to a past date. The next time a user makes a
request to a page within the domain or path that set the cookie, the
browser will determine that the cookie has expired and remove it."

Dane

unread,
Feb 7, 2011, 12:30:49 PM2/7/11
to web2py-users
Thanks.

On Feb 7, 9:28 am, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:

Dane

unread,
Feb 7, 2011, 2:11:55 PM2/7/11
to web2py-users
Ok, now I'm using this code:

for r in [request, response]:
if r.cookies.has_key('user'):
r.cookies['user']['expires'] =
datetime.datetime.strftime(datetime.datetime(2011,1,1,0,0), "%a, %d-%b-
%Y %H:%M:%S GMT")
print 'remove %s user cookie' % ('request' if r == request
else 'response')

And still my cookie isn't going away...

On Feb 7, 9:28 am, Massimo Di Pierro <massimo.dipie...@gmail.com>
wrote:

Lord Ezrik

unread,
Mar 3, 2011, 3:18:21 PM3/3/11
to web2py-users
Hey there, Dane.

I ran in to this same problem, needing to delete a cookie. Just
wanted to let you know the solution that worked for me was to ALSO set
the path just as you would when you were setting the cookie.

For instance:

- Setting the cookie:

response.cookies['test_cookie'] = 'valid'
response.cookies['test_cookie']['expires'] = 3600
response.cookies['test_cookie']['path'] = '/'

- Delete cookie

response.cookies['test_cookie'] = 'invalid'
response.cookies['test_cookie']['expires'] = -10
response.cookies['test_cookie']['path'] = '/'

Works for me. Let me know if you have any luck.

Hope that helps!

-Oliver

On Feb 7, 2:11 pm, Dane <dane.schnei...@gmail.com> wrote:
> Ok, now I'm using this code:
>
> for r in [request, response]:
>         if r.cookies.has_key('user'):
>             r.cookies['user']['expires'] =
> datetime.datetime.strftime(datetime.datetime(2011,1,1,0,0), "%a, %d-%b-
> %Y %H:%M:%S GMT")
>             print 'remove %s usercookie' % ('request' if r == request
> else 'response')
>
> And still mycookieisn't going away...
>
> On Feb 7, 9:28 am, Massimo Di Pierro <massimo.dipie...@gmail.com>
> wrote:
>
>
>
>
>
>
>
> >http://msdn.microsoft.com/en-us/library/ms178195.aspx
>
> > "You cannot directlydeleteacookieon a user's computer. However,
> > you can direct the user's browser todeletethecookieby setting the
> >cookie'sexpiration date to a past date. The next time a user makes a
> > request to a page within the domain or path that set thecookie, the
> > browser will determine that thecookiehas expired and remove it."
>
> > On Feb 7, 1:28 am, Dane <dane.schnei...@gmail.com> wrote:
>
> > > Hello,
>
> > > I've got my own user system (not using Auth), and I'm trying to
> > > implement a Remember Me option with cookies. It's working fine when
> > > the user selects 'Yes' for remember me. Setting and detecting the
> > >cookieis no problem, but I can't find a way todeletethecookiewhen
> > > the user logs out or selects 'No'. Here's my code which attempts
> > > everything I can think of.
>
> > > if request.cookies.has_key('user'):
> > >         request.cookies['user'].clear()
> > >         del request.cookies['user']
> > >         print 'remove request usercookie'
> > > if response.cookies.has_key('user'):
> > >         response.cookies['user'].clear()
> > >         del response.cookies['user']
> > >         print 'remove response usercookie'
>
> > > After running this, request.cookies.has_key('user') is still returning
> > > true and thecookie'sdata is still present.
>
> > > Ideas? Thanks.
Reply all
Reply to author
Forward
0 new messages