in reply to Seeking help to avoid duplicate cookies

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.

  • Comment on (Ovid) Re: Seeking help to avoid duplicate cookies

Replies are listed 'Best First'.
Re: (Ovid) Re: Seeking help to avoid duplicate cookies
by JK_Bean (Novice) on Jun 21, 2001 at 01:11 UTC
    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?