Let's assume I'm on sub.example.org
, how can I get the cookies of .example.org
from there? If that's not possible, is there a workaround like an hack or whatever that redirects to .example.org
and then stores the cookies?

- 338
- 2
- 10
-
@SamOnela that doesn't completely answer my questions. I'm asking also for a workaround of that. – WayneXMayersX Aug 15 '17 at 20:20
2 Answers
A web page only has visibility to the cookie with the most specific domain that matches its URL.
So if both example.org
and sub.example.org
both have a cookie named mycookie
, a web page in sub.example.org
can only access the one in the subdomain. It hides the cookie in the example.org
domain, and there's no way to access it.
But if there's only a cookie in example.org
, it will be visible to both example.org
and sub.example.org
pages.
When creating a cookie, it defaults to the full domain of the page, but the code can specify a less specific domain. So if sub.example.org
creates a cookie, it will default to domain=sub.example.org
. But the code can override this by putting domain=.example.org
in the cookie explicitly.
More details can be found in The Definitive Guide to Cookie Domains.

- 741,623
- 53
- 500
- 612
-
The cookie is in `.example.org`, how can I access it from `sub.example.org`? In Javascript. – WayneXMayersX Aug 15 '17 at 20:54
-
You don't need to do anything special if there's no more specific cookie shadowing it. It's visible automatically. – Barmar Aug 15 '17 at 21:01
-
1
-
-
-
-
@WayneXMayersX Every cookie has a name. If both `example.org` and `sub.example.org` create cookies with the same name, `sub.example.org` will only be able to access the one in `sub.example.org`. But it should still be able to access other cookies inherited from `example.org`. – Barmar Aug 16 '17 at 19:41
The cookie was set as HttpOnly
flag, and the browser made me not able to get the cookie for JavaScript.

- 338
- 2
- 10
-
1That has nothing to do with being in a subdomain. Javascript from the main domain shouldn't be able to access it, either. – Barmar Aug 16 '17 at 19:43
-
As much as I agree with @Barmar's comment, `HttpOnly` is pretty much the only thing that can prevent reading the cookie in OP's situation (except if you have 2 identical cookies set in both domains, like in Barmar's answer). – Didier L Aug 25 '20 at 10:27
-
@DidierL I wasn't contradicting that, just pointing out that the restriction is not specific to subdomains. – Barmar Aug 25 '20 at 14:31
-
1This is an answer to a different question -- the question never says that this flag is set. – Barmar Aug 25 '20 at 14:32