2013-03-10 96 views
0

我有一些工作正常的代码 - 直到我将会话初始化移入我的模块之一。现在,Cookie不再通过print-> header设置 - 所以每次运行它时都会创建一个新会话。这里的(重修剪/编辑)代码:CGI :: Session没有设置cookie

# login.pl 

use CGI; 
use CGI::Session ('-ip_match'); 
use DBI; 

use MyPM qw(&open_db &close_db &getSession); 

use strict; 
use warnings; 

my $cgi = new CGI; 
my $dbh = open_db; 
my $session = getSession($cgi, $dbh); # Call to MyPM.pm 
close_db($dbh); 

...... code ...... 

print $session->header(-type => 'application/json'); 
print $json; 

======================================= 

# MyPM.pm 

use CGI::Session ('-ip_match'); 

our @ISA = qw(Exporter); 
our @EXPORT_OK = qw(open_db close_db getSession); 

sub getSession { 

    my $cgi = shift; 
    my $dbh = shift; 

    my $CGICOOKIE = $cgi->cookie('CGISESSID') || 'x'; 
    my $lng = length($CGICOOKIE); 
    if ($lng != 32) print redirect(-URL => $home); 

    my $session = CGI::Session->new('driver:MySQL', $cgi, { Handle=>$dbh }) ; 
    return $session; 

} 

如果我改变调用getSession回到这个:

my $session = CGI::Session->new('driver:MySQL', $cgi, { Handle=>$dbh }); 

它完美地再次工作和cookie设置。

我可能会在哪里出错?

回答

0

我意识到问题在于当我检查cookie时 - 我正在检查32个字符的Cookie ......但忽略了可能丢失的事实。 对不起,我的愚蠢!