Subject: | Session gets dropped when using a HTTP redirect within CGI::Application |
Date: | Fri, 7 Nov 2008 17:42:55 -0200 |
To: | bug-CGI-Session [...] rt.cpan.org |
From: | Axo <axo [...] axo.com.ar> |
Session gets dropped when using a HTTP redirect within CGI::Application
This is driving me crazy, am I doing something wrong here?
If I set a couple of session vars, call flush (or not, its the same) and
redirect to another url, in the second run mode a new empty session is
created although
the cookie doesn't change, so I cant access any of my previous session vars.
This only happens the first time for a given client.
Its easier to understand looking at the code and the logfile (see below).
System details:
Perl, v5.8.7 built for i486-linux-gnu-thread-multi on Linux u15292264
2.6.23.16-20080211a #1 SMP
$CGI::Session::VERSION = '4.38';
$CGI::Application::VERSION = '4.06';
Code to reproduce:
Application.pm file:
package TEST::Application;
use base 'CGI::Application';
use strict;
use CGI::Application::Plugin::Session;
use Data::Dumper;
open(STDERR, q{>>},'logfiletest');
sub cgiapp_init {
my $self = shift;
$self->session_config(
CGI_SESSION_OPTIONS => [ "driver:File", $self->query,
{Directory=>'/var/tmp'} ],
SEND_COOKIE => 1,
COOKIE_PARAMS => {
-expires => "+1h"
},
DEFAULT_EXPIRY => "1h"
);
}
sub cgiapp_prerun {
my $self = shift;
my $q = $self->query;
warn "Mode: ".Dumper($q->path_info)."\n";
}
sub setup {
my $self = shift;
$self->start_mode('index');
$self->mode_param( path_info => -1 );
$self->run_modes([qw/
index
ren
/]);
}
#######################################################################################
#### Run Modes
#######################################################################################
sub index {
my $self = shift;
my $session = $self->session;
$session->param('md5','123456');
$session->param('logged_user', 'pepe');
$session->flush();
warn "index mode session=".$session->id;
my $logged_user = $self->check_session;
warn "doing redir...";
$self->redir("/indextest.pl/ren");
return "index mode session=".$session->id;
}
sub ren {
my $self = shift;
my $session = $self->session;
my $logged_user = $self->check_session;
warn "ren logged user=".$logged_user;
return "ren mode session=".$session->id;
}
#######################################################################################
#### Utility functions
#######################################################################################
sub check_session {
my $self = shift;
return unless my $session = $self->session;
warn "session id=".$session->id;
warn "dataref=".Dumper($session->dataref());
warn "cookie =CGISESSID".$self->query->cookie('CGISESSID');
return unless $session->param('md5');
return $session->param('logged_user') if $session->param('md5');
}
sub redir {
my $self = shift;
my $url = shift;
$self->header_type('redirect');
$self->header_props(-url=>$url);
return "Redirecting to $url";
}
1;
indextest.pl File:
#!/usr/bin/perl -w
use strict;
use CGI::Carp qw(fatalsToBrowser);
use TEST::Application;
open(STDERR, '>>logfiletest');
my $app = new TEST::Application;
$app->run();
Resulting logfiletest:
[Fri Nov 7 14:09:04 2008] indextest.pl: Mode: $VAR1 = '';
[Fri Nov 7 14:09:04 2008] indextest.pl:
[Fri Nov 7 14:09:05 2008] indextest.pl: index mode
session=8931907b0822e3696087a3797ff79fe9 at TEST/Application.pm line 52.
[Fri Nov 7 14:09:05 2008] indextest.pl: session
id=8931907b0822e3696087a3797ff79fe9 at TEST/Application.pm line 79.
[Fri Nov 7 14:09:05 2008] indextest.pl: dataref=$VAR1 = {
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_ETIME' => 3600,
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_ID' =>
'8931907b0822e3696087a3797ff79fe9',
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_ATIME' =>
1226084945,
[Fri Nov 7 14:09:05 2008] indextest.pl: 'logged_user' => 'pepe',
[Fri Nov 7 14:09:05 2008] indextest.pl: 'md5' => '123456',
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_REMOTE_ADDR' =>
'190.18.144.128',
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_CTIME' =>
1226084945
[Fri Nov 7 14:09:05 2008] indextest.pl: };
[Fri Nov 7 14:09:05 2008] indextest.pl: cookie
=CGISESSIDf5f4635a8899929d4649d53074d890c2 at TEST/Application.pm line 81.
[Fri Nov 7 14:09:05 2008] indextest.pl: doing redir... at
TEST/Application.pm line 56.
[Fri Nov 7 14:09:05 2008] indextest.pl: Mode: $VAR1 = '/ren';
[Fri Nov 7 14:09:05 2008] indextest.pl:
[Fri Nov 7 14:09:05 2008] indextest.pl: session
id=6f5b87b3c56709591d34e2e8920654fa at TEST/Application.pm line 79.
[Fri Nov 7 14:09:05 2008] indextest.pl: dataref=$VAR1 = {
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_ETIME' => 3600,
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_ID' =>
'6f5b87b3c56709591d34e2e8920654fa',
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_ATIME' =>
1226084945,
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_REMOTE_ADDR' =>
'190.18.144.128',
[Fri Nov 7 14:09:05 2008] indextest.pl: '_SESSION_CTIME' =>
1226084945
[Fri Nov 7 14:09:05 2008] indextest.pl: };
[Fri Nov 7 14:09:05 2008] indextest.pl: cookie
=CGISESSIDf5f4635a8899929d4649d53074d890c2 at TEST/Application.pm line 81.
[Fri Nov 7 14:09:05 2008] indextest.pl: Use of uninitialized value in
concatenation (.) or string at TEST/Application.pm line 68.
[Fri Nov 7 14:09:05 2008] indextest.pl: ren logged user= at
TEST/Application.pm line 68.
Steps to reproduce:
1- delete all sessions from /var/tmp
2- go to http://webserver/indextest.pl
In the logfile you can see that the session id changes after the redirect
even if the cookie its the same. Upon inspection of the session dir, 2
different files (sessions) where created.