How to Delete a Cookie?

T

tryg.olson

Hello -

I managed to get a cookie set. Now I want to delete it but it is not
working.

Do I need to do another 'set-cookie' in the HTTP header? I tried
(code below setting expires to 0) and it didn't work.
c = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
c["mycook"]["expires"] = 0
print c

In case its useful, here is how I set my cookie:
c = Cookie.SimpleCookie()
c["mycook"] = "Tryg"
c["mycook"]["expires"] = 60*60*24
c["mycook"]["comment"] = ""
c["mycook"]["path"] = "/"
c["mycook"]["domain"] = ""
c["mycook"]["secure"] = ""
print c

Thanks
Tryg
 
M

Mike Driscoll

Hello -

I managed to get a cookie set.  Now I want to delete it but it is not
working.

Do I need to do another 'set-cookie' in the HTTP header?  I tried
(code below setting expires to 0) and it didn't work.
c = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
c["mycook"]["expires"] = 0
print c

In case its useful, here is how I set my cookie:
c = Cookie.SimpleCookie()
c["mycook"] = "Tryg"
c["mycook"]["expires"] = 60*60*24
c["mycook"]["comment"] = ""
c["mycook"]["path"]    = "/"
c["mycook"]["domain"]  = ""
c["mycook"]["secure"]  = ""
print c

Thanks
Tryg

Well, if you know where the cookie file is stored, you should be able
to do this:

os.remove(path)

Mike
 
M

mk

Hello -

I managed to get a cookie set. Now I want to delete it but it is not
working.

Why struggle with this manually? Isn't it better to learn a bit of
framework like Pylons and have it all done for you (e.g. in Pylons you
have response.delete_cookie method)?

Regards,
mk
 
J

Jose C

c["mycook"]["expires"] = 0

Set ["expires"] using the following format to any time less than
current (which causes the browser to delete the cookie).
Here's a function I use to return a cookie expiry timestamp, negative
values passed in result in cookie being deleted.

def cookie_expiry_date(numdays):
""" Returns a cookie expiry date in the required format. -ve
value in = kill cookie.
`expires` should be a string in the format "Wdy, DD-Mon-YY
HH:MM:SS GMT"
NOTE! Must use [expires] because earlier IE versions don't
support [max-age].
"""
from datetime import date, timedelta
new = date.today() + timedelta(days = numdays)
return new.strftime("%a, %d-%b-%Y 23:59:59 GMT")

Usage:
c["mycook"]["expires"] = cookie_expiry_date(-10) # any negative value
will remove cookie


HTH,
JC
 
T

tryg.olson

c["mycook"]["expires"] = 0

Set ["expires"] using the following format to any time less than
current (which causes the browser to delete the cookie).
Here's a function I use to return a cookie expiry timestamp, negative
values passed in result in cookie being deleted.

def cookie_expiry_date(numdays):
""" Returns a cookie expiry date in the required format. -ve
value in = kill cookie.
`expires` should be a string in the format "Wdy, DD-Mon-YY
HH:MM:SS GMT"
NOTE! Must use [expires] because earlier IE versions don't
support [max-age].
"""
from datetime import date, timedelta
new = date.today() + timedelta(days = numdays)
return new.strftime("%a, %d-%b-%Y 23:59:59 GMT")

Usage:
c["mycook"]["expires"] = cookie_expiry_date(-10) # any negative value
will remove cookie

HTH,
JC


Jose C's piece of code works to delete the cookie as does setting
["expires"]=0 but ONLY as long as I also set the path. Why is this?
So what would be the best way to do this. I tried reading in the
existing cookie (b), creating a new cookie (c) with all the same
values except for the "expires" but this did not get my cookie
deleted.

b = Cookie.SimpleCookie(os.environ["HTTP_COOKIE"])
c = Cookie.SimpleCookie()
c[cookieName] = b[cookieName].value
c[cookieName]["expires"] = 0
c[cookieName]["path"] = b[cookieName]["path"]
print c
 
J

Jose C

c["mycook"]["expires"] = 0
Set ["expires"] using the following format to any time less than
current (which causes the browser to delete the cookie).
Here's a function I use to return a cookie expiry timestamp, negative
values passed in result in cookie being deleted.
def cookie_expiry_date(numdays):
    """ Returns a cookie expiry date in the required format.  -ve
value in = kill cookie.
    `expires` should be a string in the format "Wdy, DD-Mon-YY
HH:MM:SS GMT"
    NOTE!  Must use [expires] because earlier IE versions don't
support [max-age].
    """
    from datetime import date, timedelta
    new = date.today() + timedelta(days = numdays)
    return new.strftime("%a, %d-%b-%Y 23:59:59 GMT")
Usage:
c["mycook"]["expires"] = cookie_expiry_date(-10)  # any negative value
will remove cookie

Jose C's piece of code works to delete the cookie as does setting
["expires"]=0 but ONLY as long as I also set the path.  Why is this?

The path specifies which directory the cookie is active. Usually the
path is set to /, which means the cookie is valid throughout the
entire domain, but you could set it to /mydir meaning it would only be
active for pages within /mydir.
So what would be the best way to do this.  I tried reading in the
existing cookie (b), creating a new cookie (c) with all the same
values except for the "expires" but this did not get my cookie
deleted.

To kill the cookie, simply set a cookie with the same name (and path)
and a past date (or 0, although IIRC there was some issue with 0 being
used on a particular browser some time ago, can't remember which on at
the moment) as an 'expires' parameter, is enough to tell the browser
to kill an existing cookie with that same name, regardles of it's
value or previous expiry, etc.

Basically, when you set a cookie, the browser overwrites the previous
one of the same name if it exists. If not, it creates a new cookie
with your specified parameters.

So in your case, when you want to kill the cookie you set previously,
you should be able to just set a cookie of the exact same name, path
and 'expire' it appropriately, and the browser takes care of the
rest. Don't worry about assigning it's previous value, the browser is
just going to delete it anyway.

JC
 
J

Jose C

To kill the cookie, simply set a cookie with the same name (and path)

Actually you may not even need to specify the path. Just the name and
expires param should do the trick. Haven't played with cookies in a
while so try with and without the path.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,262
Messages
2,571,048
Members
48,769
Latest member
Clifft

Latest Threads

Top