views:

651

answers:

2

On a subdomain -- a.test.com -- I'm trying to read the cookies set at .test.com. If I use document.cookie in JS, all i'm getting are the cookies from a.test.com. What is the syntax or route to read the cookies from .test.com?

I'm pretty certain you can read up -- from sub domain to fqdn -- but you can not read down -- fqdn to sub domain.

Thanks!

+1  A: 

While setting the cookies at test.com, make sure that you specify the cookie domain as ".test.com".

For example:

your_key_name=your_key_value;domain=.test.com;expires=...
Ates Goral
Ates, that's the thing, I don't want to do any setting. I just want to read. All the setting is being done server side from another application.
zmonteca
Ates is saying that, unless the cookie has been set properly, with the domain prefixed by a period, you won't be able to read the cookie from a subdomain
Gareth
So are you saying if it is set properly, then document.cookie will implicitly read all the cookies from .a.test.com, as well as, .test.com?
zmonteca
yes, that is correct. There is no special syntax
Gareth
Thanks Gareth for chiming in and elaborating on my rather terse answer.
Ates Goral
A: 

No, it´s the more probable the other way around. That you can read down that is.

Think of web hosts that lets you have a site as a subdomain. It´s make no sense that you should be able to read(/write) their cookies but it make some sense that they can read yours.

anddoutoi