views:

23

answers:

1

I have a cookie "A" set at a domain at bc.test.com(all urls are changed to protect company info).

I also have a cookie "B" set at a domain of .test.com.

I also have a Javascript file hosted at bc.test.com/_include.

Now, in my host file, I have setting to point bc.test.com to a development server.

But now, when I try reading cookie "A" using document.cookie, I am not able to access the cookie. I can however read cookie "B". I though as long as the cookie is set at the same level domain, I should be able to read it - right?

Seems to be a very simple problem, yet I am confounded by how it's currently working.

A: 

To read a cookie, you need:

  • Have the same subdomain (ok, all in test.com)
  • Have the same location ("folder")

Check this first of all.


EDIT

Ex: If your you have bc.test.com/index.php you can read cookies in all paths in bc.test.com. But if you have bc.test.com/includes/index.php, you can read only cookies from folder include.

Look this article for more information about cookie path.

The path gives you the chance to specify a directory where the cookie is active. So if you want the cookie to be only sent to pages in the directory cgi-bin, set the path to /cgi-bin. Usually the path is set to /, which means the cookie is valid throughout the entire domain.

Topera
are you saying a file that is stored in _include/script.js won't be able to read a cookie for a file that is in test/help.js although they're set to the same domain?
Dhana
No. I'm saying that the page that reads the cookie must be in the same domain. A'll Edit my answer.
Topera