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

Hi, i am trying to implement user sessions in my web application to add security and enable information to be passed from an inital form to mysql queries in later forms. I have been using the following code
use CGI::Session; use CGI; #set up a session $cgi = CGI->new; $session = CGI::Session->new("driver:File", $cgi, {Directory=>"./tmp"} +); $sid = $session->id(); $cookie = $cgi->cookie(CGISESSID => $session->id); print $cgi->header( -cookie=>$cookie ); $sid = $cgi->cookie("CGISESSID") || undef; $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});
However this is not working, here are a few of the errors that I am getting
Global symbol "$cgi" requires explicit package name at /....ls/result. +cgi line 49. Global symbol "$session" requires explicit package name at /....ls/res +ult.cgi line 50. Global symbol "$cgi" requires explicit package name at /....ls/result. +cgi line 50. Global symbol "$sid" requires explicit package name at /....ls/result. +cgi line 51. Global symbol "$session" requires explicit package name at /....ls/res +ult.cgi line 51. Global symbol "$cookie" requires explicit package name at /....ls/result.cgi line 53. Global symbol "$cgi" requires explicit package name at /....ls/result. +cgi line 53.
Can anybody see why this is not working? Thanks

Replies are listed 'Best First'.
Re: cgi session
by andyford (Curate) on Dec 11, 2006 at 21:27 UTC

    Looks like you need to use "my" to scope your variables.

    use CGI::Session; use CGI; #set up a session my $cgi = CGI->new; my $session = CGI::Session->new("driver:File", $cgi, {Directory=>"./tm +p"}); my $sid = $session->id(); my $cookie = $cgi->cookie(CGISESSID => $session->id); print $cgi->header( -cookie=>$cookie ); $sid = $cgi->cookie("CGISESSID") || undef; $session = new CGI::Session(undef, $sid, {Directory=>'/tmp'});

    non-Perl: Andy Ford

      Thanks andy, that seemed to work. Would someone be able to give me a quick explanation of what this actually does, i pieced it together from online examples and trial and error but dont fully understand how it works. Thanks again