Perl Session? HOW!

Welp, first thing… the code I posted before was crappy… Sorry, was in a hurry when I posted it.

Page 1 Code


use strict;
use CGI::Session;
use CGI;
use DBI;
use CGI::Carp qw(fatalsToBrowser);

my $cgi = new CGI;
$CGI::DISABLE_UPLOADS = 1;          # Disable uploads
$CGI::POST_MAX        = 512 * 1024; # limit posts to 512K max


#create a new session
my $session = new CGI::Session(undef, undef, {Directory=>'c:/temp/session/'});


#set session to expire in 1 hour
$session->expire("+1h");
	
#store something
$session->param("days","Friday");
		
#write to disk
$session->flush();


#create the cookie with a 1hour limit..
my $cookie = $cgi->cookie(-name=>"CGISESSID",
		                           -value=>$sid,
		                           -expires=>"+1h",
		                           -path=>"/");
		
#set the cookie..
print $cgi->header(-cookie => $cookie );


Page 2 code


use strict;
use CGI::Session;
use CGI;
use DBI;
use CGI::Carp qw(fatalsToBrowser);

#create a new CGI object.
my $cgi = new CGI;
$CGI::DISABLE_UPLOADS = 1;          # Disable uploads
$CGI::POST_MAX        = 512 * 1024; # limit posts to 512K max


#try to retrieve cookie.
my $sid = $cgi->cookie("CGISESSID") || undef;


#create session... If I retrieved a previous session id, reconnect to it.
#if not, create a new session.
my $session = new CGI::Session(undef, $sid, {Directory=>'c:/temp/session/'});


print $session->param("days");

Now, depending on what kinda of privacy setting you may have set in your browser, the cookie may or may not be set. So you might want to check that and see if there are any issues there.

I mainly use Mozillay Firefox, but have used Mozilla, Netscape, Opera, and IE… and for all of them, you can look at what cookies are set and what values are contained in them. Might want to investigate that as well and see if the cookie is being set right.

tabo