CGI :: Minimal的问题

时间:2015-07-07 14:43:14

标签: perl cgi

我现有的代码与vanilla cgi一起使用

#!/usr/bin/env perl
use strict;
use CGI;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session;

my ( $session, $cgi);

$cgi = new CGI();
$cgi->charset('UTF-8');

$session = new CGI::Session( "driver:File;serializer:Storable",
    $cgi, { Directory => '../home/tmp' } );
$session->expire( 'authorized', '1440m' );

切换到CGI :: Minimal会导致CGI :: Session错误输出

#!/usr/bin/env perl
use strict;
use CGI::Minimal;
use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session;

my ( $session,  $cgi);

my $cgi = CGI::Minimal->new;

$session = new CGI::Session( "driver:File;serializer:Storable",
    $cgi, { Directory => '../web/tmp' } ) or or die CGI::Session->errstr();
$session->expire( 'authorized', '1440m' );

错误

Can't call method "expire" on an undefined value at /var/webserver/iris/htdocs/index.cgi line 13.

修改:添加or die CGI::Session->errstr()

new(): failed: query object CGI::Minimal=HASH(0x9916a64) does not support cookie() and param() methods: 

不确定是什么导致这种情况,非常感谢任何见解。

1 个答案:

答案 0 :(得分:0)

由于CGI :: Minimal确实相当小,因此您必须自己处理Cookie并将手动检索的session_id(而不是cgi对象)传递给CGI :: Session构造函数。

use warnings;
use strict;

use CGI::Cookie;
use CGI::Minimal;

use CGI::Carp qw/fatalsToBrowser warningsToBrowser/;
use CGI::Session;

my $cgi = CGI::Minimal->new();

my %cookies = CGI::Cookie->fetch;

my $session_id;

if( defined $cookies{'SESSION_ID'} ){
   $session_id = $cookies{'SESSION_ID'}->value;
}

my $session = CGI::Session->new(
  "driver:File;serializer:Storable",
  $session_id,
  { Directory => '../web/tmp' }
  ) or die CGI::Session->errstr();

$session->expire( 'authorized', '1440m' );

my $session_cookie = CGI::Cookie->new(-name => 'SESSION_ID',-value => $session->id());

print $session->header(-cookie=>[$session_cookie]);

print 'ok with session_id: ' . $session->id();