5

I have a cookie named cookie1 on domain domain1.com with path / and several pages afterwards I get another cookie with the same name and the same domain but on a different path /path. When a browser requests pages underneath /path, which of the cookies will it send? Which one of them will count as the good one?

PoByBolek
  • 3,775
  • 3
  • 21
  • 22
walla
  • 293
  • 1
  • 2
  • 16
  • 3
    possible duplicate of [How to handle multiple cookies with the same name?](http://stackoverflow.com/questions/4056306/how-to-handle-multiple-cookies-with-the-same-name) – PoByBolek May 12 '14 at 14:30

2 Answers2

4

The correct answer is Nate's answer from How to handle multiple cookies with the same name?

TLDR summary: Nothing is guaranteed as it is not fully defined. We may get only the cookie under /path or both of them. And the order we will get them will be also unknown.

Dan Atkinson
  • 11,391
  • 14
  • 81
  • 114
walla
  • 293
  • 1
  • 2
  • 16
3

I'm running into the same issue myself. From my research, it looks like if you have two cookies with the same name, but different paths, the cookie that will be used is the one that matches the path of the current page.

For example, if you have two cookies:

'name=myCookie; path=/'
'name=myCookie; path=/Main/'

and you are on the page /Main/index.html, the second cookie will be used.

Also, the more specific the path, the higher the precedence. This SitePoint article states:

If multiple cookies of the same name match a given request URI, one is chosen by the browser. The more specific the path, the higher the precedence.

So if you are on the page /Main/something/whatevzdude/index.html, you will also get the second cookie.

Fillip Peyton
  • 3,637
  • 2
  • 32
  • 60
  • Check out the comment to my answer (that links to a duplicated question), it has a good answer. Basically you are right but it can be browser dependent so don't count on it. – walla Dec 21 '14 at 15:45
  • Makes sense. I'm hoping to go another route. Here's my [related question](http://stackoverflow.com/questions/27610090/updating-cookie-expiration-only-if-path-matches-page), in case it interests you. – Fillip Peyton Dec 22 '14 at 20:41