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

Hi, I can't seem to retrieve my cookie even though I can see it stored on disk. Here is the sub I use to store the cookie:
sub setCookie($) { my($session) = @_; my $query = new CGI; $sid = $session->id(); $cookie = $query->cookie(-name => "CGISESSID", -value => $sid, -expires => '+1h', -path => '/tmp/Sessions'); print $query->header(-cookie=>$cookie); }

And the code to retrieve the cookie:

sub retrieveSession($) { my ($query, $session) = @_; $sid = $query->cookie(CGISESSID=>$session->id) || undef; $sid = substr($sid, 10, -8); $session = new CGI::Session(undef, $sid, {Directory=>'/tmp/Session +s'}); $sid = $session->id(); $session = getUser($session, $sid); return $session; }

Where getUser returns the login name and password of the user logged in from a database. The getUser function isn't the problem its the  $sid = $query->cookie(CGISESSID=>$session->id) || undef;.

If I don't pass the session then it won't find the cookie although its right there in /tmp/Sessions. ARGH!!!

Replies are listed 'Best First'.
Re: Cookie Problems
by chromatic (Archbishop) on Sep 29, 2004 at 20:37 UTC

    I'm afraid I don't understand your code at all. If you already know the session ID, why are you passing that to $query->cookie()? I also don't believe that retrieveSession() works at all; you have a prototype mismatch that I'd expect to throw warnings, at least.

    If you rewrite the function more like this:

    sub retrieveSession { my ($query) = @_; my $sid = $query->cookie('CGISESSID') || ''; $sid = substr($sid, 10, -8); my $session = CGI::Session->new(undef, $sid, {Directory=>'/tmp/Ses +sions'}); return getUser($session, $sid); }

    ... does it work better?

      Sorry about the confusion. Basically, I have a member's page. I want them to have access to the restricted areas of the sites once they register. In order to track their login I'm using CGI::Session. But say, they leave the page, I set a cookie in /tmp/Sessions with the session id.

      When they return to the member's area it will check if the cookie is found and then recreate that old session with the username and session id.

      The only reason I pass the session ID is because I was testing it from the command prompt. Should have removed that before I posted..

      I tried implementing your solution, but that doesn't work either. I go to my site, login, and go to a member area page that checks to see if a user is logged in and it redirects me back to the login/register page as it cannot find the session.

      Same as before. Thanks for the help though...still thinking about it.

        A couple questions you might want to ask about the code:
        • Are you dumping out the contents of these variables to see where everything first goes wrong?
          I always use Data::Dumper for this, I don't know how I would live without it:
          print Dumper <my variable>
        • Do you get anything back after you call query->cookie?
        • Also, I just want to double check that you have examined the cookie in your browser cookie manager to make sure it really is there.
        • And the file in your tmp directory is also looking as it should?
        --rachel