views:

115

answers:

2

Hi folks,

I'm using Firefox 3.6.8 for these tests.

I'm setting a cookie within the response headers of my web app using:

Set-Cookie: session=7878dfdsfjsdf89sd89f8df9

This does not seem to override the session Cookie.


When a request is performed instead Firefox even sends duplicate cookies:

Cookie: session=7d75cd8f55895cbccb0d31ee07c7afc0; 
        session=671e8448a5cebda0442005a186cf69a3; 
        4cb6f2d75c9ffc8916cb55bcbaafecd8

What is going on?? Any ideas would be great!! =)


This is quite disastrous in my case... if someone could explain what's going on it would really help me out!

+3  A: 

You can delete the previous cookie using the response object.

response.delete_cookie(cookie_key)

The set of cookies is available via the request object in the request.COOKIES dictionary, and you can obtain the key from there.

Since you're using Django, here's how you might do this in the view function:

def my_view(request):
    # do some work and create a response object
    response = HttpResponse(some_content)

    # first delete any previously set cookie named "session"
    if 'session' in request.COOKIES:
        response.delete_cookie('session')

    # set the new cookie
    response.set_cookie('session', <cookie value goes here>')
    return response
ars
@ars thanks, how do I get an http server to replace the Cookie though? I won't have control over the user's machine when a web app goes live.
RadiantHex
@ars: btw I'm sending in the 'Set-Cookie' in manually through Django, but Firefox will not replace the old cookies no matter what...
RadiantHex
This is will make my hair go grey before time =)
RadiantHex
RadiantHex, see the update for how you might do this on the server side in a django view.
ars
+2  A: 

If you don't specify the path or domain for a cookie when setting it, it defaults to the current path and current hostname. If you then go ahead and try setting the same cookie name from a URL with a different path or hostname, it will add a new cookie instead of replacing the old one.

I suspect what you want to do is just set a cookie with a global path for your site and for your entire domain. So something like this:

Set-Cookie: session=7878dfdsfjsdf89sd89f8df9; path=/; domain=.mysite.com
Marc Novakowski
@Marc I think you have solved my problem... I was literally going crazy =D
RadiantHex
@Marc: can I set the Cookie to cover all paths?
RadiantHex
@Marc: I love you!!!!!! :3
RadiantHex
Yes, setting a path to just / will cover all paths on your site
Marc Novakowski