in reply to Cookie Problems

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?

Replies are listed 'Best First'.
Re^2: Cookie Problems
by intranetman (Acolyte) on Sep 29, 2004 at 21:09 UTC
    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
        In answer to your questions:

      • Are you dumping out the contents of these variables to see where everything first goes wrong?
      • Thanks for the helpful hint. That is extremely useful. When I try to retrieve a cookie from /tmp/Sessions I get  $VAR1 = undef; and then my custom error message. It looks as if the cookie are being stored on the server but not in the user's browser as they should.

      • Do you get anything back after you call query->cookie?
      • If I pass the session id back then yes I will get something. However, if i do not pass the session id then I I get  $VAR1 = undef;.

      • 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.
      • I checked in Mozilla's Cookie Manager and its not there. Also, in IE cookie/temp folders and it isn't there.

      • And the file in your tmp directory is also looking as it should?
      • Yes the cookie is stored as cgisess_a983c8302e7a678a2e53c65e8bd3316 in /tmp/Sessions on the server. Stored as the following:

           $D = {"_SESSION_ETIME" => undef,"_SESSION_ID" => "29456deb010b73d47c7e12af13c7ec2d","log_password" => "31z0a41PqrlmW","~logged-in" => 1,"log_name" => "intranetman","_SESSION_REMOTE_ADDR" => undef,"_SESSION_CTIME" => "1096560508","~profile" => "31z0a41PqrlmW,"_SESSION_ATIME" => "1096560508","_SESSION_EXPIRE_LIST" => {"~logged-in" => 3600}};

        Thanks everyone for all the helpful hints. I finally solved the problem last week. Indeed, this is somewhat embarassing. Going through the perl documentation I found a small line that stated that you were not suppose to send content-type headers on a page if you were trying to set a cookie. That being said, since there was already a header set for that page the cookie could not be created. Argh!!

        Also, I figured that it would probably be beneficial to leave the path field out of the cookie declaration to deal with multiple OS.