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

Hi, i am setting a session dat in 1 file and trying to retrieve in another file. Not able to get the stored data. Need help. i can see the session file being created in the folder mentioned.

session1.pl

#!"\TBD\xampp\perl\bin\perl.exe" use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; use CGI; use CGI::Session; my $cgi = new CGI; # Object initialization: my $session = new CGI::Session("driver:File", undef, {Directory=>' +d:/TBD'}); # getting the effective session id: my $sessionid = $session->id(); #print "$sessionid \n"; my $cookie = $cgi->cookie(CGISESSID => $session->id); print $cgi->header( -cookie=>$cookie ); # storing data in the session $session->param('my_name', 'Sherzod'); #printf ("<a href=\"session2.pl?%s=%s\">click me</a>", 'CGISESSID', $s +ession->id); print<<OUTPUT; <html> <body> <br><br> <form action="session2.pl" METHOD="POST"> <input type="hidden" name="CGISESSID" value="$sessionid"> <input type="submit" > </form> </body> </html> OUTPUT exit (0);
session2.pl
#!"\TBD\xampp\perl\bin\perl.exe" use CGI::Carp qw(fatalsToBrowser); use strict; use warnings; use CGI; use CGI::Session; my $cgi = new CGI; # Object initialization: print "Content-type: text/plain; charset=iso-8859-1\n\n"; my $sid = $cgi->cookie('CGISESSID') || $cgi->param('CGISESSID') || + undef; my $session = new CGI::Session(undef, $sid, {Directory=>'d:/TBD'}) +; my $name = $session->param("my_name"); exit (0);

Replies are listed 'Best First'.
Re: cgi::session issue with session data retrival
by kcott (Archbishop) on Oct 25, 2010 at 23:51 UTC

    Firstly, add use strict; and use warnings; to your scripts. When you've done this, Perl will start helping you. You'll need to fix a few things, e.g. changing $cgi = new CGI; to my $cgi = new CGI; - I can see a couple more like that.

    Next, take a look at CGI::Session::Tutorial.

    If you're still experiencing difficulties, post your updated code as well as any relevant output.

    -- Ken

      Have updated the code as per the suggestion (as updated in the question). Now i am seeing the below error in the second file while retrieving the session data. Can't locate auto/CGI/Session/File/expire.al in @INC (@INC contains: D:/TBD/xampp/perl/lib D:/TBD/xampp/perl/site/lib .) at D:/TBD/xampp/perl/lib/CGI/Session.pm line 258

        Looking through the documentation for CGI::Session, I see that there are a number of formats for new(); none of which equate to what you have in session2.pl:

        my $session = new CGI::Session(undef, $sid, {Directory=>'d:/TBD'});

        Compare that with the equivalent line in session1.pl.

        Also, I'd recommend using the Class->new form instead of the new Class form. Take a look at Indirect Object Syntax in perlobj for a discussion of this. It's a good habit to get into and may save you some grief down the track.

        -- Ken