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

Ok, here's the question: How do I get what's in a cookie that I set? Say I want to put the cookie into a variable called "$cookie". Do I use: $cookie = $ENV{'HTTP_COOKIE'}; ? Cause if that's it, it is not working. The cookie is set properly, yet I cannot retrieve it! Any help is welcome!

Replies are listed 'Best First'.
Re: Cookie Function Thingy
by btrott (Parson) on Mar 31, 2000 at 03:17 UTC
    What you wrote above should work, yes. There are several variables here: perhaps the cookie was set for the wrong domain (not the domain for which you're trying to retrieve the cookie); perhaps it was set for the wrong path; perhaps it wasn't set at all. Are you able to find the cookie in your cookies file? Are you running your CGI script on a particular ISP?

    Why don't you put this code in your script:

    for my $key (keys %ENV) { print $key, ": ", $ENV{$key}, "<br>\n"; }
    This should print out all of the values in %ENV-- you're using the right variable, but perhaps this will give a hint as to why it's not working.

    Also, please post the code that you're using to set the cookie.

    And I'm not trying to point you towards CGI.pm, but what doesn't work about it?

Re: Cookie Function Thingy
by turnstep (Parson) on Mar 31, 2000 at 04:57 UTC
    Your code looks okay. When I went to the page, it set three cookies for me. Try turning on cookie notification if your browser supports it. (Some show only the name and value, but some (IE I believe is one) show all the details) The best domain to set it to in your case is "thegamepage.tripod.com" (no leading period)

    The browser, when it loads your page, looks at all of it's cookies, and then uses the domain to figure out whether or not to send each one. If it is sent, the web server reads it into the environment variables, which perl accesses through the hash %ENV. Just some background...

RE: Cookie Function Thingy
by Anonymous Monk on Mar 31, 2000 at 03:26 UTC
    Thanks for that Code! Here is some info: 1. My address is: http://thegamespage.tripod.com 2. The script is at: http://thegamespage.tripod.com/cgi-bin/cookie/test2.pl 3. The cookie is visible in my netscape cookie file. 4. I have had the domain set to: tripod.com, .tripod.com, thegamespage.tripod.com, .thegamespage.tripod.com. and even nothing (so it sets it itself) 5. That code you gave me shows no cookies set. 6. The 2 scripts I use are both in the same directory. Any other ideas?
      What is the path set to? I think you should set it to '/'.

      Also, I can't actually view the source of the URL you sent along (of the script to add/test the cookie)... could you post just the bit that sets the cookie?

RE: Cookie Function Thingy
by Anonymous Monk on Mar 31, 2000 at 03:28 UTC
    The script to add the cookie is located at: http://thegamespage.tripod.com/cgi-bin/cookie/test.pl
RE: Cookie Function Thingy
by Anonymous Monk on Mar 31, 2000 at 03:41 UTC
    just give me a sec
RE: Cookie Function Thingy
by Anonymous Monk on Mar 31, 2000 at 03:47 UTC
    Here it is: $name="cookie_name"; $value="cookie_value"; #$cur_date = "Tue, 28 Mar 2000 06:10:23 -0500"; $expires_date = "Fri, 31 Mar 2000 13:49:23 -0500"; $path = "/"; $domain = ".tripod.com"; print "Content-type: text/html\n"; print "Set-Cookie: $name=$value; expires=$expires_date; path=$path; domain=$domain\n\n";
RE: Cookie Function Thingy
by Anonymous Monk on Mar 31, 2000 at 03:10 UTC
    AND PLEASE DON'T DIRECT ME TO CGI.PM! THAT DOESN'T WORK FOR ME AT ALL!
Re: Cookie Function Thingy
by stephen (Priest) on Mar 31, 2000 at 03:39 UTC
    Anonymous...

    I don't think we can proceed much further without seeing your code, or at least the relevant sections. Seems to me that the question has more to do with cookies than Perl, but we can't tell without some code, since the stuff you've told us seems OK. (Make sure you enclose it in <CODE></CODE> tags.)

    Also, to echo btrott above... what do you mean when you say that CGI.pm doesn't work for you? Do you mean that it gives an error, that it's not on your site, or just that you don't like it?

    stephen

      Well, from that later email, it seems he/she is on tripod. And tripod is pretty limited in the modules they provide, I think. There's a list, and it doesn't include CGI::Cookie. So perhaps that explains it (it does include CGI.pm, but I think the poster would need CGI::Cookie as well in order to use cookies, so that rules that out).
        Hmmm.... no, CGI by itself handles cookies fine with its cookie() method. You don't need CGI::Cookie in addition, I don't think... correct me if I'm wrong, smoking something, or both.