JK_Bean has asked for the wisdom of the Perl Monks concerning the following question:

I am getting cookies with the SAME name which my code cannot handle, since it doesn't know which is the most recent. I am now having the perl code on my site keep a log of all my user's cookies for my site in order to try to sort out what's going on. I have found the problem exists whether you write the cookies via perl/cgi from the server end, or javascrypt on the client PC.

Even code written in javascrypt with "; path=/" at the end still can eventually yeild 2 cookies.

Does anyone know the mechanism that is causing this, and what to do to stop it? I was trying to avoid embedding an incrementing serial number in each cookie, but it's starting to look like that's my only solution. I hate unclean code.

Replies are listed 'Best First'.
(Ovid) Re: Seeking help to avoid duplicate cookies
by Ovid (Cardinal) on Jun 21, 2001 at 00:31 UTC

    Generally, it's a bad idea to have multiple cookies with the same name on one site. It causes a lot of confusion, as you are discovering. One thing you can do, if your programs have different paths, is to limit the path info in the cookie as carefully as possible, to avoid overlap or overwriting (think of it as scoping for cookies).

    The real solution, though, is to thoroughly examine the reason for your cookies and determine what happened with your implementation. If every single page on your site sets a login cookie, for example, then this is really a poor implementation.

    Another strategy is to check for the cookie before setting another one. And remember, the cookie specification only allows for 20 cookies per server or domain. While a browser might accept more, you should assume the lowest common denominator. If you try to set that 21st cookie, the 1st will be lost.

    Cheers,
    Ovid

    Join the Perlmonks Setiathome Group or just click on the the link and check out our stats.

      Perhaps my post was missleading.

      I am not intentionally trying to create cookies with the same name. I only need one cookie and it has a unique name, however when I inspect the value of $ENV{'HTTP_COOKIE'} using the perl code, I get more than one instance of the cookie, which may or may not have the same value. For example the value of $ENV{'HTTP_COOKIE'} might be something like: foo=bar1; foo=bar2

      An example of the java code segment that I use to write the cookie:

      var tkm = new Date(); tkm.setTime(tkm.getTime() + 96*3600000); document.cookie = "foo=bar1; expires=" + tkm.toGMTString() + "; path=/ +";

      Also note that I always access the cookie from the same location. Any clues?

Re: Seeking help to avoid duplicate cookies
by jreades (Friar) on Jun 21, 2001 at 00:36 UTC

    I hate to be an unhelpful jerk, but what you're describing is unlikely at best, and most probably impossible.

    Try this JS code:

    <html> <head> <script language="JavaScript"> function setCookie() { document.cookies = "foo=bar"; } function getCookies() { window.alert("Cookies: " + document.cookies); } </script> </head> <body> <form> <p align="center"><input type="button" value="Set A Cookie" onClick="s +etCookie();"></p> </form> </body> </html>

    No matter how many times you click setCookie there's only that one cookie there. By definition cookies have to have unique names, AFAIK.

    It sounds like your code is doing something unexpected in terms of how it sets/reads cookies.

    Try posting some code and we can take a look...

    And on a final note, if you're setting the cookie via JavaScript and reading via Perl then the error could be quite simple:

    • You read cookies in JavaScript as if they were a single string,
    • but you write to them automagically via a simple assignment
    • so if you're saying document.cookies += cookieNames you're going to get some interesting results (i.e. multiple cookies that, apparently, have the same name)

    Try it by changing "=" to "+=" in my code.

    HTH

      By the way, I wrote a JSAN module for handling cookies from JS: HTTP.Cookies
      Actually HTH, this IS an IE error. it happems when the same cookie name is set from different pages, even in the same folder It seems M$ have broken the cookie code. when reading you always get the last one, or none if the page reading is different from the original one that set it the only way around this is to use a while loop on the cookie in question, expiring it until it no longer exists, then setting it again Paul
Re: Seeking help to avoid duplicate cookies
by Anonymous Monk on Aug 19, 2009 at 06:55 UTC
    Cookie names are not unique, and multiple cookies with the same name are quite valid, given they have different paths or domains. Browsers generally handle this correctly and send back all cookies - it is the job of the server app to specify domain or path if they want to avoid duplicate cookies.