http://qs321.pair.com?node_id=6372

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

Does anyone know of any modules that can handle cookies well? Dont say CGI::Cookies because I never use the CGI module and i don't want to load it just to use cookies (Ahem slow and big).
  • Comment on Recommended modules for handling cookies?

Replies are listed 'Best First'.
Re: Recommended modules for handling cookies?
by plaid (Chaplain) on Mar 29, 2000 at 02:46 UTC
    There are some debugging steps you'll need to go through to find out what the problem is. Some of the things I'd start with:

    1. Turn on the cookie warnings in your browser. This will let you make sure that the cookie is being set, and should also let you see what it's being set to.

    2. Check your cookies file (wherever that may be) and check for the cookie you just set. I don't know on windows, but on unices, the file is generally in ~/.netscape/cookies

    3. Telnet into your webserver once the cookie has visibly been set, port 80, and type 'GET / HTTP/1.0<enter><enter>'. Any cookies being returned to you will show up in the header.

    4. Any other rudimentary checks, such as making sure that cookies aren't turned off, or something of the likes.

    If nothing there is conclusive, respond with your test results and it'll have to be dug into deeper.

Re: Recommended modules for handling cookies?
by setantae (Scribe) on Mar 29, 2000 at 02:21 UTC
Re: Recommended modules for handling cookies?
by btrott (Parson) on Mar 29, 2000 at 02:18 UTC
    What isn't working? Are the cookies not getting sent back from the browser?

    Send some example code, if you can. Just a short snippet containing the code you're using to set the cookie.

    Have you read the cookie specification? From that document:

    When searching the cookie list for valid cookies, a comparison of the domain attributes of the cookie is made with the Internet domain name of the host from which the URL will be fetched. If there is a tail match, then the cookie will go through path matching to see if it should be sent. "Tail matching" means that domain attribute is matched against the tail of the fully qualified domain name of the host. A domain attribute of "acme.com" would match host names "anvil.acme.com" as well as "shipping.crate.acme.com". Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us". Any domain that fails within one of the seven special top level domains listed below only require two periods. Any other domain requires at least three. The seven special top level domains are: "COM", "EDU", "NET", "ORG", "GOV", "MIL", and "INT". The default value of domain is the host name of the server which generated the cookie response.
Re: Recommended modules for handling cookies?
by Anonymous Monk on Mar 29, 2000 at 03:04 UTC
    OK,
    1. Sorry for screwing up with the - which should have been _. That's what my code has.
    2. The cookie does get set, with a later expiration date than today.
    3. The cookie is in my netscape cookies file.
    4. Here's that snippet of code:
    $cookie = $ENV{HTTP_COOKIE}; $name="cookie_name"; $value="cookie_value"; $cur_date = "Tue 28 Mar 2000 13:49:23 -0500"; $expires_date="Wed, 29 Mar 2000 13:49:23 -0500"; $path = "/"; $domain = ".tripod.com"; print "Content-type: text/html\n"; print "Set-cookie: $name=$value; date=$cur_date; expires=$expires_date +; path=$path; domain=$domain"; print " <html><body>$cookie</body></html> ";
    HELP! What is fishy to me are my dates! And what exactly is that -0500 doing there? (I got this code from somewhere else)
      I only see one carriage return after the HTTP header. Are you sure you've pasted/typed that in correctly? In your case, you should have the Content-Type header, then a carriage return (which you have), then the Set-Cookie header, then *two* carriage returns... I only see one in your code.

      Again, though, I can't emphasize enough: use CGI::Cookie! Does Tripod not make that available? Is that why you're not using it?

      Also, I've never heard of the "date" attribute in a Set-Cookie header... it's not in the specification. That's probably not the problem, since the cookie is getting placed in your cookies file correctly. But still.

      So this cookie did show up in your cookies file? What's the domain listed as in the cookies file? Is it ".tripod.com"?

      No real new insight here.. It looks like a newline problem to me. But to answer the other part of your question, one of the standards for writing time involves following the time itself by the offset from GMT. In your case, -0500 would put you 5 hours west of GMT, or somewhere on Eastern Time.
Re: Recommended modules for handling cookies?
by Anonymous Monk on Mar 29, 2000 at 23:02 UTC
    OK, now what about this: I am now trying to use CGI.pm. But now here'es the new problem: I get an error that says that when i call on the module with use CGI; It tells me that "CGI not found at @INC" and that "@INC contains '.'"! So what's wrong now?
      You're on Tripod, yes? According to their docs, it looks like you need to load the Tripod modules into your cgi-bin before you can use them. It looks like it loads CGI.pm into the directory, as well.

      It looks like they have instructions here.

Re: Recommended modules for handling cookies?
by Anonymous Monk on Mar 29, 2000 at 02:45 UTC
    OK, say my address is: http://sub.domain.com. When setting cookie, what do I use for a domain address? domain.com, .domain.com, sub.domain.com, or .sub.domain.com? And to get a cookie, I use the $ENV{HTTP-COOKIE} command/variable, right?
      Just try using "sub.domain.com" as the domain. If that's what your domain always is (ie. you don't have other domains like "foo.domain.com" that you want to set the same cookie for), just use that.

      And it's "HTTP_COOKIE", not "HTTP-COOKIE". It's an underscore ("_").

      And, finally, you really should be using CGI::Cookie. From the docs:

      To set cookies:

      # Create new cookies and send them $cookie1 = new CGI::Cookie(-name=>'ID',-value=>123456); print header(-cookie=>[$cookie1]);
      To fetch cookies:
      %cookies = fetch CGI::Cookie; $id = $cookies{'ID'}->value;
      Read the documentation for more info, particularly on setting the domain (look at the code setantae posted).
      I don't want to sound patronising here, but are you sure that's a subdomain and not a host?

      Let's assume it's a host, so your domain is really domain.com, so that's what you put in for the domain.
      To retrieve a cookie, leave out the -value part if you're using the above method.

      setantae@eidosnet.co.uk|setantae|www.setantae.uklinux.net

      Straight from the CGI::Cookie man page
      2. domain This is a partial or complete domain name for which the cookie is valid. The browser will return the cookie to any host that matches the partial domain name. For example, if you specify a domain name of ".capricorn.com", then Netscape will return the cookie to Web servers running on any of the machines "www.capricorn.com", "ftp.capricorn.com", "feckless.capricorn.com", etc. Domain names must contain at least two periods to prevent attempts to match on top level domains like ".edu". If no domain is specified, then the browser will only return the cookie to servers on the host the cookie originated from.
Re: Recommended modules for handling cookies?
by Anonymous Monk on Mar 29, 2000 at 03:45 UTC
    gotta go now, will check back tomorrow, after I try using that module.