2013-03-10 98 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 ......但忽略了可能丟失的事實。 對不起,我的愚蠢!