1

I am observing, on both Firefox and IE, that if I have a cookie 'x' on the domain a.b.c.com, and also have a cookie with the same name 'x' on domain a.b.com, then when I look at the value of document.cookie on the a.b.c.com domain, it shows both cookies. I would like to see just the cookie from the a.b.c.com domain, and not the one from the other domain. (I'm assuming this occurs because one domain is the same as the other one, with an additional segment on the hostname.) Is there a way to do this?

I don't have control over the contents of the cookie, and I don't see anything obvious in those contents that distinguishes one domain from the other.

Bruce
  • 8,202
  • 6
  • 37
  • 49
  • there's some mistake in your question, a.b.com is not the parent of a.b.c.com – chris166 Jul 07 '09 at 17:42
  • I don't know the correct wording for a domain that is the same as another domain, but with an additional segment on the hostname. Hopefully the example makes it clear. I changed the wording to replace 'parent' with some more detailed parenthetical text. – Bruce Jul 07 '09 at 17:47
  • Are you talking about cookies relating to subdomains. Where you have a cookie stored on sub.example.com and example.com You only want to see the cookie on sub.example.com? – Jesse Dorsey Jul 07 '09 at 17:55
  • Yes, exactly - I only want to see the cookie for the subdomain. How can I tell them apart? – Bruce Jul 07 '09 at 17:57
  • 1
    This sounds like a decent hint for your problem: http://stackoverflow.com/questions/348282/php-cookie-domain-subdomain-control – ylebre Jul 07 '09 at 17:59
  • Interesting, but unfortunately I don't have control over where and how the cookie gets created. – Bruce Jul 07 '09 at 18:03

1 Answers1

3

You don't have access to the domain of the cookie in Javascript.

"When [the cookie] attribute is read, all cookies are returned as a single string, with each cookie's name-value pair concatenated into a list of name-value pairs, each list item being separated by a ';' (semicolon)."
W3C

When you read a cookie, you only have access to the name/value pairs, and cannot determine any other information about it. If you require things such as when it was set, what domains it was set for, or anything else, you have to store it inside the cookie value.

Since you cannot set the cookies, you need another method to do what you're attempting.

Community
  • 1
  • 1
Ian Elliott
  • 7,588
  • 5
  • 35
  • 42