(possibly) dumb question about passing cookies between subdomains

One of the sites I run has a login page on a different subdomain from the rest of the site. Let’s say, for the sake of discussion, that the login page is here:


and the rest of the content’s all here:


I need something on the main site (a poll) to recognize the username, and the only way I can possibly do this with what I have available is to set a cookie on the login page, and retrieve it in the main site.

On the login page, I have tried this:

and this:

Neither of these work. Is there something I’ve forgotten about the way cookies set in one subdomain of foo.com can be readable in another? Or a dumb mistake? Or (uh oh) can it just not be done with the situation I’ve got here?

And no, I can’t do this with server-side scripting in this case…

I think it needs to be .foo.com. (Leading dot.) But I haven’t done cookie stuff for a while, so I may be remembering wrong.

Nope, that’s not quite it… tried that and it’s still not going through. There must be a way as cookies from all over the .foo domain are showing up… so I guess best bet is to take a look at some of these pages and see what they’re doing differently. Will post what I find out.

Sorry to take so long to try this and get back on it… was forced to back-burner this for a few days.

Actually it is a leading dot, but it will only work between the same levels within the domain name.

By setting the cookies domain to .mysite.com, you can share cookies between:

But the cookies will not be shared with

The domain level must be equal to share cookies.

Dang… I was afraid that might be what was going wrong. Looks like I’m SOL.

Thanks for letting me know, though!

You could perhaps just carry the username over via a query string to your desired url, and then if that query string exists set a cookie in the next domain.

For example, you could set the cookie username on the register.foo.com domain, and within any page in the register domain or any other *.foo.com domain, you could get the cookie, and append it to the links URLS. eg


Then once in the subdomain you can get the value from the query string via javascript and set the new cookie in foo.bar.foo.com.