php_nub_qq
php_nub_qq

Reputation: 16055

Edit a dot cookie from javascript

This is driving me crazy. I am setting a cookie with a SetCookie header sent from PHP, without a domain.

setcookie('test','val',0,null,null,false,false);

Then firefox ( and I suppose other browsers ) automatically picks up the domain and makes the cookie available to all sub-domains with the annoying .domain.com. However I want to edit that cookie from JavaScript, how is that possible?

If I put

document.cookie = 'test=val';

Then I get two cookies with the name of test, one with domain domain.com ( set from javascript ) and the other one .domain.com ( sent from php ). I am being even further more confused by the fact that document.cookie and the Cookie header itself contain no information that could help distinguish the two cookies!

Upvotes: 0

Views: 168

Answers (1)

Barmar
Barmar

Reputation: 781726

Put:

document.cookie = "test=val; domain=.domain.com";

in the Javascript. The default is for the domain to be the exact domain of the web page, you need to put .domain.com in explicitly to override it.

The Set-Cookie: header that came from PHP must have contained domain=.domain.com

Upvotes: 1

Related Questions