in reply to Re^2: Cookie Problems
in thread Cookie Problems

A couple questions you might want to ask about the code: --rachel

Replies are listed 'Best First'.
Re^4: Cookie Problems
by intranetman (Acolyte) on Sep 30, 2004 at 16:40 UTC
    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.

        Yes, when I set the cookie I print the header. Here is the sub for that:

        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);

        Then attempt to retrieve the cookie here:

        sub retrieveSession($) { my ($query, $session) = @_; $sid = $query->cookie(CGISESSID=>$session->id) || undef; $sid = substr($sid, 10, -8); $session = new CGI::Session(undef, $sid, {Directory=>'/'}); return getUser($session, $sid); }
      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.
Re^4: Cookie Problems
by intranetman (Acolyte) on Oct 05, 2004 at 17:17 UTC
    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.