0

我有一些运行良好的代码 - 直到我将会话初始化移动到我的一个模块中。现在不再通过 print->header 设置 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。

我可能哪里出错了?

4

1 回答 1

0

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

于 2013-03-11T10:57:45.103 回答