lshettyl
lshettyl

Reputation: 8181

Setting a cookie on the TLD from a subdomain

I am pretty new to this cookie stuff. I have a requirement where I need to set a cookie on the TLD (www.example.co.uk) from one of the subdomains (sub.example.co.uk) using Javascript. First of all, is it possible? if yes, how do I go about doing that? Any help on this would be highly appreciated.

Thanks, L

Upvotes: 3

Views: 10777

Answers (2)

Gerben
Gerben

Reputation: 16825

You could do it on the server side, by loading an image from the main domain name which is actually a script setting the cookie.

so in the HTML code of sub.example.com add <img src="http://www.example.com/spacer.php?newcookie=test" />

then in spacer.php you read the get 'newcookie' and set it using setcookie (PHP) and include a spacer image.

It's a bit hacky, but it works.

Upvotes: 0

gpojd
gpojd

Reputation: 23085

I think you are confused about the definition of a TLD. You cannot set a cookie on a TLD. You can try to set a cookie on a different domain, but the browser may or may not accept it (look into the P3P header). I would set it in a shared domain. In your example, it would be .example.co.uk and would work for www.example.co.uk and sub.example.co.uk.

Try this (untested):

document.cookie = "name=value; expires=date; path=path; domain=.example.co.uk";

Upvotes: 7

Related Questions