# (possibly) dumb question about passing cookies between subdomains

**URL:** <https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164>\
**Category:** Factual Questions\
**Created:** [September 11, 2008, 9:48pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164 "2008-09-11T21:48:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Engywook](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Engywook](https://boards.straightdope.com/u/Engywook)\
**Post date:** [September 11, 2008, 9:48pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164/1 "2008-09-11T21:48:09Z")

</div>

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:

> **[foo.com](http://www.foo.com/?not_found=registration.foo.com)**

and the rest of the content’s all here:

> **[foo.com](http://www.foo.com/?not_found=foo.bar.foo.com)**

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:

> [@](#):
>
> function setCookie(value)  
> {  
> …  
> document.cookie=“username=”+escape(value)+“; **[domain=foo.com](http://domain=foo.com);** path=/;”;  
> …  
> }

and this:

> [@](#):
>
> function setCookie(value)  
> {  
> …  
> document.cookie=“username=”+escape(value)+“; **[domain=foo.bar.foo.com](http://domain=foo.bar.foo.com);** path=/;”;  
> …  
> }

Neither of these work. Is there something I’ve forgotten about the way cookies set in one subdomain of [foo.com](http://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…

---

<div class="post-metadata">

**Author:** ![arseNal](https://avatars.discourse-cdn.com/v4/letter/a/ecae2f/32.png) [@arseNal](https://boards.straightdope.com/u/arseNal)\
**Post date:** [September 11, 2008, 10:23pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164/2 "2008-09-11T22:23:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Engywook](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Engywook](https://boards.straightdope.com/u/Engywook)\
**Post date:** [September 17, 2008, 5:40pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164/3 "2008-09-17T17:40:50Z")

</div>

> [@arseNal](#):
>
> 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.

---

<div class="post-metadata">

**Author:** ![lexi](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lexi/32/6806_2.png) [@lexi](https://boards.straightdope.com/u/lexi)\
**Post date:** [September 17, 2008, 6:18pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164/4 "2008-09-17T18:18:59Z")

</div>

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:  
[register.mysite.com](http://register.mysite.com)

> **[Website Hosting - Mysite.com](http://www.mysite.com)**
>
> For affordable website hosting packages, go to MySite.com. You'll find complete and reliable website hosting with a range of prices and options.

> **[My Free website on MySite](http://frank.mysite.com)**
>
> Visit my new website on MySite.

[jimbo.mysite.com](http://jimbo.mysite.com)

But the cookies will not be shared with

> **[Website Hosting - Mysite.com](http://mysite.com)**
>
> For affordable website hosting packages, go to MySite.com. You'll find complete and reliable website hosting with a range of prices and options.

or  
[anne.frank.mysite.com](http://anne.frank.mysite.com)  
[jumbo.jimbo.mysite.com](http://jumbo.jimbo.mysite.com)

The domain level must be equal to share cookies.

---

<div class="post-metadata">

**Author:** ![Engywook](https://avatars.discourse-cdn.com/v4/letter/e/bc8723/32.png) [@Engywook](https://boards.straightdope.com/u/Engywook)\
**Post date:** [September 17, 2008, 6:22pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164/5 "2008-09-17T18:22:37Z")

</div>

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

Thanks for letting me know, though!

> [@lexi](#):
>
> 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:  
> [register.mysite.com](http://register.mysite.com)  
> [www.mysite.com](http://www.mysite.com)  
> [frank.mysite.com](http://frank.mysite.com)  
> [jimbo.mysite.com](http://jimbo.mysite.com)
> 
> But the cookies will not be shared with  
> [mysite.com](http://mysite.com)  
> or  
> [anne.frank.mysite.com](http://anne.frank.mysite.com)  
> [jumbo.jimbo.mysite.com](http://jumbo.jimbo.mysite.com)
> 
> The domain level must be equal to share cookies.

---

<div class="post-metadata">

**Author:** ![lexi](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/lexi/32/6806_2.png) [@lexi](https://boards.straightdope.com/u/lexi)\
**Post date:** [September 17, 2008, 6:45pm UTC](https://boards.straightdope.com/t/possibly-dumb-question-about-passing-cookies-between-subdomains/463164/6 "2008-09-17T18:45:50Z")

</div>

> [@Engywook](#):
>
> 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](http://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

```auto

http://foo.bar.foo.com?username=COOKIE_USERNAME_VAL

```

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](http://foo.bar.foo.com).
