我是 perl-cgi 的新手。我正在使用 cgi-perl 创建会话并设置 cookie。但我无法检索会话的值。我也在粘贴page1和page2的代码。任何人都可以编辑代码并告诉我错误在哪里。我什至可以将会话存储在代码中提到的那个目录中,并且值也存在。
第 1 页:
#!c:/perl/bin/perl.exe
use strict;
use CGI qw/:standard/;
use CGI::Session qw/-ip-match/;
use CGI;
use DBI;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
print "created Session";
my $session = new CGI::Session(undef, undef, {Directory=>'c:/temp/session/'});
#setting session to expire in 1 hour
$session->expire("+1h");
#store something
$session->param("value1","yakub");
#write to disk
$session->flush();
my $cookie1 = new CGI::Cookie(-name=>'CGISESSID',-value=>$session->id);
print "Set-Cookie: $cookie1\n";
第2页:
#!c:/perl/bin/perl.exe
use CGI::Cookie;
use CGI::Session;
use CGI;
use DBI;
use CGI::Carp qw(fatalsToBrowser);
print "Content-type: text/html\n\n";
my $cgi = new CGI;
#try to retrieve cookie.
$sid = $cgi->cookie('CGISESSID') || $cgi->param('CGISESSID') || undef;
$session = load CGI::Session(undef, $sid, {Directory=>'c:/temp/session/'});
$name = $session->param("value1");
print $name;