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

Hi there,

I've already asked once how to use session to store a simple value and applied the answer to my code, also gone through the CGI:: Session tutorial a few times and still cannot get my code to work.

I've not touched Perl in a few years so sorry if i'm making a simple mistake.

I have two files, sesssion1.pl and session2.pl - when you click on the link printed out by session1.pl it should take you to session2.pl and print out the value stored in the session. Why is this not working?

SESSION1.PL
use CGI::Session; use CGI; my $cgi = new CGI; my $session = new CGI::Session("driver:File", $cgi, {Directory=>'/tmp' +}); $cookie = $cgi->cookie(CGISESSID => $session->id ); print $cgi->header(-cookie=>$cookie); $session->param('f_name', 'Sherzod'); print '<a href="session2.pl">Page 2</a>';


SESSION2.PL
use strict; use CGI::Session; use CGI::Carp qw(fatalsToBrowser warningsToBrowser); use CGI; my $cgi = new CGI; my $sid = $cgi->cookie("CGISESSID") || undef; my $session = new CGI::Session("driver:File", $sid, {Directory=>'/tmp' +}); my $cookie = $cgi->cookie(CGISESSID => $session->id ); print $cgi->header(-cookie=>$cookie); my $name = $session->param("l_name"); print $name;


Any help greatly appreciated. Kind Regards Steve

Replies are listed 'Best First'.
Re: Sessions In With CGI::Session
by osunderdog (Deacon) on Apr 23, 2007 at 11:57 UTC

    I hate it when this happens. (If I'm right...)

    $session->param('f_name', 'Sherzod');

    You're setting the parameter "f_name"

    my $name = $session->param("l_name");

    And asking for the parameter "l_name".

    Hazah! I'm Employed!

Re: Sessions In With CGI::Session
by shmem (Chancellor) on Apr 23, 2007 at 13:03 UTC
    Apart from the "f_name/l_name" glitch, you can avoid all that cookie mumbo jumbo by just saying
    print $session->header();

    since CGI::Session sets the cookie for you. You should use load instead of new in your second file. Consult the docs.

    --shmem

    _($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                                  /\_¯/(q    /
    ----------------------------  \__(m.====·.(_("always off the crowd"))."·
    ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
      Thanks for the help guys, much appreciated.