Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Chrome not setting cookie path to root

I am setting a cookie in Javascript using the following code :

setCookie('cart_items','product_name');


function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

But the cookie path is not set to root (/) in Chrome. Instead it gets set to the path from where the web page is being executed !!

I tested with IE and FF. It works fine with both these browsers ....

What might be wrong with Chrome or Is it the problem with cookie creation code i am using??

In Chrome ( 16.0.912.63 )

Path: /xxxxxxxx/xxxxxxx

in FF ( 6.0 )

Path: /

in IE (9)

Path: /

like image 529
Sandy505 Avatar asked Dec 17 '11 04:12

Sandy505


People also ask

Why is my cookie not getting set?

This happens with the session cookies are disabled. Restart your server and then try to set the cookie. They should immediately be available. Show activity on this post.

Can you set cookie on a different path?

You can't access cookies from a different path - otherwise it would be a security hole.

How do I set a cookie path?

cookie = "username=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/"; This statement will delete a cookie named “username” if one exists. Some browsers will not allow the deletion of a cookie if the path is not specified. Therefore, it is important to always specify the path when working with cookies.

What is default path for cookies?

For a cookie set at uri-path /a/b , the "right-most" / is the one before the b . The algorithm says to stop there, hence the default cookie path is /a and therefore the cookie should get sent to https://example.com/a .


1 Answers

The reason this happens is because chrome doesn't allow setting cookies on local files by default. See this answer for more information: https://stackoverflow.com/a/347997/1324019 (text from answer)

Chrome doesn't support cookies for local files (or, like Peter Lyons mentioned, localhost*) unless you start it with the --enable-file-cookies flag. You can read a discussion about it at http://code.google.com/p/chromium/issues/detail?id=535.

*Chrome does support cookies if you use the local IP address (127.0.0.1) directly. so in the localhost case, that could be an easier workaround.

like image 129
Mansfield Avatar answered Oct 25 '22 09:10

Mansfield