[cgiapp] Changing CGI::Session name within a CGI::Application

Mark Knoop mark at rawcane.net
Wed Feb 6 09:56:14 EST 2008


>
>> Hi
>>
>> I am trying to use the CGI::Application::Plugin::Session but I am getting 
>> a bit confused with what objects are what.
>> ...
>
> Ok. I have made some progress in pinnign this down and it seems I have a 
> problem with the session object.
>
> This code recreates the scenario.
>
> package GSystem::Test;
> use base 'CGI::Application';
> use strict;
>
> use CGI::Carp qw(fatalsToBrowser);
> use CGI::Application::Plugin::Session;
>
> sub cgiapp_init {
>
>    my $self = shift;
>  my $session_path;
>
> CGI::Session->name('lsid');
>    $self->session_config(
>       CGI_SESSION_OPTIONS => [ "driver:File", $self->query, { 
> Dir=>'D:\sessions\\' } ], # running on windows/IIS
>       DEFAULT_EXPIRY      => '+5m',
>       SEND_COOKIE         => 0,
>    );
>
> }
>
> sub setup {
>
> my $self = shift;
>
>    $self->start_mode('no_lsid');
>
>    $self->run_modes(
>  'got_lsid' => 'got_lsid',
>  'no_lsid' => 'no_lsid'
>    );
>
>
> }
>
> sub cgiapp_prerun {
>
> my $self = shift;
>
> my $session = $self->session;
>
>    if ($self->query->param('lsid') ) {
>  $self->prerun_mode('got_lsid');
> } else {
>  $self->session->param('rsid', 'SOME_VALUE');
>  $self->prerun_mode('no_lsid');
>    }
> }
>
> sub got_lsid {
>
> my $self = shift;
>
> my $lsid = $self->session->id;
> my $rsid = $self->session->param('rsid');
>
> my $output = <<EOF;
>
> <html><body>Got lsid $lsid - rsid is $rsid</body></html>
>
> EOF
>
> return $output;
>
> }
>
> sub no_lsid {
>
> my $self = shift;
>
> my $lsid = $self->session->id;
>
> my $output = <<EOF;
>
> <html><body>New lsid is $lsid - <a href = 
> "http://my.url.com/test.pl?lsid=$lsid">Link</a></body></html>
>
> EOF
>
> return $output;
> }
>
> 1;
>
> If I call this with no parameters it returns a link with the new session 
> id as a value to lsid. If I click on this then it runs the got_lsid run 
> mode but has not successfully retrieved the rsid value from the session 
> data and has created a new session id.
>

I just tried recreating this using CGI::Session directly and it worked as I 
had hoped, even with cookies disabled in my browser.

################################

use strict;
#use warnings;
use CGI qw/:standard/;
use CGI::Session;

my $cgi = new CGI;

CGI::Session->name('lsid');

my $session  = new CGI::Session("driver:File", $cgi, 
{Directory=>'D:\sessions\\'});
$session->expire('+5m');

my $lsid = $session->id;

my $rsid;

if ($cgi->param('lsid') ) {
 $rsid = $session->param('rsid');

 print $cgi->header;
 print <<EOF;
 <html><body>Got lsid $lsid - rsid is $rsid</body></html>
EOF
} else {
 $session->param('rsid','SOME_VALUE');
 print $cgi->header;
 print <<EOF;
 <html><body>New lsid is $lsid - <a href = 
"http://my.url.com/test_noca.pl?lsid=$lsid">Link</a></body></html>
EOF
}

exit;

##################

The notable difference is that I can see the session data files appearing in 
D:\sessions whereas they were not with my previous attempt. Any thoughts?

Mark




More information about the cgiapp mailing list