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

I've read through as many Cookie postings as I could find, but I still don't know why the following fails to work (I don't seem to be able to recieve the cookie):
File A (login script): ... $c = $query->cookie(-name=>'ID', -value=>( username=>$username, password=>$password ) ); print $query->redirect(-location=>$goodurl,-cookie=>$c); exit 0; File B (page viewer): %cookies = fetch CGI::Cookie; if(%cookies ne undef) { if($cookies{'ID'} ne undef) { my %hash = $cookies{'ID'}->value; $username = $hash{'username'}; $password = $hash{'password'}; } else { # Cry, because this is where I keep ending up. # If I don't have check for the undef, # $cookies{'ID'}->value actually kills the script... } }
Um...help?

Replies are listed 'Best First'.
Re: Crashing Cookie Code
by Masem (Monsignor) on Jun 19, 2001 at 17:07 UTC
    You may want to try to use the module Data::Dumper to make sure that %cookies look as you are defining them. (That is:
    use Data::Dumper; ... ... print Dumper %cookies;
    ). Also in your code:
    my %hash = $cookies{'ID'}->value; $username = $hash{'username'}; $password = $hash{'password'};
    The docs for CGI::Cookie suggest that value() will return a hashref if the original cookie is feed a hashref, as opposed to returning a hash. What you probably want is :
    my $hashref = $cookies{'ID'}->value; $username = $$hashref{'username'}; $password = $$hashref{'password'};


    Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
      I've done some extra checking and:

      1) My browser definetly receives the cookie.
      2) The second script receives no cookies. I used the Dumper thing, I've also tried a foreach (keys %cookies) loop, and I've even tried send a scalar value (instead of the hash).

      Is there a way to check if it's a problem with the server config, or if the browser isn't sending the cookie back? (Unlikely, since I've tried it from Linux and Windows using Netscape and IE - both of which behave fine at other cookie-enabled sites.)

      P.S. Code now looks like this:

      %cookies = fetch CGI::Cookie; print "<br>Fetched Cookie<br>\n"; print Dumper %cookies; # Prints nothing!!! print "<br>Just dumped cookie hash.<br>\n"; if(%cookies ne undef) { # We get here, so %cookies is defined print "Keys: "; foreach (keys %cookies) # Finds no keys!!! { print; print " "; } print "<br>---<br>"; if($cookies{'ID'} ne undef) { # Tried as a plain hash, hashref, with and without '->value' my $hashref = $cookies{'ID'};#->value; $username = $$hashref{'username'}; $password = $$hashref{'password'}; print "Username: $username<br>"; print "Password: $password<br>"; } else { # We keep ending up here!!!! print "Damn!<br>\n"; } }
        Ok, at this point, I'd drop back to CGI.pm and try using the raw_cookie() function to just see if anything is getting through (you'll have to create a CGI object to use this). raw_cookie's output will be rather messy, but it should have at least something if the cookie is being sent.
        Dr. Michael K. Neylon - mneylon-pm@masemware.com || "You've left the lens cap of your mind on again, Pinky" - The Brain
        I'd like to thank everyone for their help - the problem is now fixed. For the sake of others with this problem, here's what was going on:

        For some reason, while the browser was receiving a Cookie (I turned on the "Warn Me" option), it wouldn't actually save it when I tried to send a Hash. I fixed that by sending 2 cookies, each with a simple scalar value.

        However, the other problem was that while the CGI docs claim that if the "path" parameter is left out it gets set to "/" (allowing all your scripts access to the cookie), I found that it actually set it to the full script path (which didn't work for me since I was using 2 scripts). I had to manually set to path to "/".

        These were both solutions I tried before posting, but I guess that it wasn't at the same time. And while no one actually literally suggested this solution, some of the advice prevented other bugs from cropping up. Thanks again!
Re: Crashing Cookie Code
by voyager (Friar) on Jun 19, 2001 at 17:17 UTC
    You have:
    my %hash = $cookies{'ID'}->value;
    which I don't find in the CGI documentation, which says:
    %hash = cookie('ID');