in reply to Re: Cookie Problems
in thread Cookie Problems

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.

Replies are listed 'Best First'.
Re^3: Cookie Problems
by geekgrrl (Pilgrim) on Sep 29, 2004 at 22:09 UTC
    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}};

        Without seeing the rest of your code, I can only speculate, but are you printing a header anywhere before this point? You can print only one and if you want to set a cookie, you have to do it there.

        Apologies if you already know this; it's a common mistake that trips up everybody at least once, though.

        Hey,
        So you need to first work on successfully setting the browser cookie. Then you may find the rest of your code works for you.

        So I think the problem is that you are misusing the -path parameter when creating a new cookie.

        From the perldoc for CGI::Cookie: (emphasis my own)
        -path points to a partial URL on the current server. The cookie will be returned to all URLs beginning with the specified path. If not specified, it defaults to '/', which returns the cookie to all pages at your site.
        It is NOT where the cookie gets stored. It is specifying which URLs get the cookie. I'd delete it, and see if that helps.
      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.