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

Fellow Monasterians,

Note: I posted a similar question the other day, but have simplified it somewhat.

Anyway, I'm on a mission to understand CGI::Sessions but I'm missing something.

My guess is that I'm not not using new CGI::Session() properly. Here is a reduction of a much larger application. I hope someone can spot the issue here, without lots of extraneous code.

Problem: it looks like I'm setting a new session and losing my value: logged-in. Obviously, I'd like to keep the session the same, and the value.

my ($logged_in, $session_id) = write_session(); # Dumper to file shows # $VAR1 = 1; # $VAR2 = 'ae83ec8591d8579c1955d3e798dde74b'; my ($logged_in, $session_id) = check_session(); # Dumper to file shows # $VAR1 = undef; # $VAR2 = '1209a2de95b31a59d338f70458702860'; sub write_session { my $session = new CGI::Session(); print $session->header(); $session->param('logged_in' => 1); $session->expire('+2h'); return ( $session->param('logged_in'), $session->id() ); } sub check_session { my $session = new CGI::Session(); return ( $session->param('logged_in'), $session->id() ); }

Thanks in advance.

—Brad
"The important work of moving the world forward does not wait to be done by perfect men." George Eliot

Replies are listed 'Best First'.
Re: CGI::Session appears to be overwriting itself
by Anonymous Monk on Feb 17, 2009 at 01:48 UTC
Re: CGI::Session appears to be overwriting itself
by jethro (Monsignor) on Feb 17, 2009 at 01:44 UTC

    Could it be that the cookie is not stored on the client yet because your script on the server hasn't finished sending the html-page? So when the script asks for the cookie on the second CGI::Session call the client doesn't know the cookie yet and a new session is created

      No, cookie set via http headers get set immediately (I've never seen a http client do different).

      Thought of this being the issue, but I thought this stored the cookie:

      print $session->header();

      Yes? No? Thanks.

      Update: I stopped the process after write_session() and I did have a cookie written (according to the cookies dialog in Firefox). But whe allowed to proceed to the check_session a new cookie and session are written.

      —Brad
      "The important work of moving the world forward does not wait to be done by perfect men." George Eliot
Re: CGI::Session appears to be overwriting itself
by Anonymous Monk on Feb 17, 2009 at 02:59 UTC
    You may require rest :)
    #!/usr/bin/perl -- use strict; use warnings; use CGI(); use CGI::Session(); my( $oneid, $twoid); { my $one = CGI::Session->new or die CGI::Session->errstr; my $two = CGI::Session->new or die CGI::Session->errstr; $one->param( is => 'is one'); $two->param( is => 'is two'); $oneid = $one->id; $twoid = $two->id; } { my $bob = CGI::Session->load( $oneid ) or die CGI::Session->errstr +; my $dod = CGI::Session->new( $twoid ) or die CGI::Session->errstr; warn 'bob : ', $bob->param('is'); warn 'dod : ', $dod->param('is'); } { my $cgi = CGI->new( { CGISESSID => $oneid }); my $ses = CGI::Session->load( $cgi ) or die CGI::Session->errstr; warn 'ses : ', $ses->param('is'); } __END__ bob : is one at cgi-session-ex.pl line 22. dod : is two at cgi-session-ex.pl line 23. ses : is one at cgi-session-ex.pl line 29.