我在使用CGI::Session在MySQL数据库中存储会话时遇到了一个问题。
这是一段代码片段
#!/usr/bin/perl
use CGI;
use CGI::Session;
use CGI::Session::Driver::mysql;
use DBI;
use DBD::mysql;
use Net::LDAPS;
require '../include/include.pl';
$LDAP_SERVER = 'my.test.ldap.example.com';
$LDAP_SSL_PORT = '636';
$LDAP_BASE = 'ou=users,dc=example,dc=com';
$ldap = Net::LDAPS->new($LDAP_SERVER, port=> $LDAP_SSL_PORT)
or die "Unable to create LDAP object because: $! \n";
$dbh = DBI->connect("DBI:mysql:host=$db_host;database=$db_name",$db_user,$db_pswd)
or die "Unable to connect to database: \"$DBI::errstr\" $! \n";
$q = CGI->new;
$usr = $q->param('usr') || undef;
$userDN = "uid=$usr,$LDAP_BASE";
if($usr) {
$pwd = $q->param('pwd');
$ldapMsg = $ldap->bind($userDN, password=>$pwd);
$result = $ldap->code;
if ($result == 0) {
$session = CGI::Session->new('driver:mysql', undef,
{ TableName=>'car_sessions',
IdColName=>'id',
DataColName=>'a_session',
Handle=>$dbh})
or die "Unable to create session because: $!";
$session->expire('+1h');
$session->param(-name=>'car_login', -value=>$usr);
$sess_cookie = $q->cookie(-name=>'CGISESSID', -value=>$session->id, -expires=>'+1h', -path=>'/hr_car/');
$login_cookie = $q->cookie(-name=>'car_login', -value=>$usr, -expires=>'+1h', -path=>'/hr_car/');
print $q->header(-cookie=>[$sess_cookie, $login_cookie], -location=>'manage.cgi');
}LDAP绑定正确,cookies设置正确,但我的会话表中没有显示任何内容!
我能做错什么呢?
发布于 2011-11-12 10:34:00
我认为问题出在auto-flushing being unreliable上。在自动刷新发生之前,DBI句柄在超出作用域时存在一个明显的问题,因此在设置完会话并删除它之后,请调用$session->flush。
你可以通过对$dbh和朋友使用file-scoped lexicals而不是全局变量来缓解这个问题,Perl也许能够以正确的顺序清理它们,这是一个好主意。
PS打开strict和warnings并声明所有这些变量。你的问题很可能是由一个打字错误引起的,而且你永远都不会知道。
https://stackoverflow.com/questions/8100733
复制相似问题