0

我是 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;                            
4

1 回答 1

0

You said in your comment that you received the cookie. This is true and false at the same time.

Your problem is the first line:

print "Content-type: text/html\n\n";                    
print "created Session";                 
...
print "Set-Cookie: $cookie1\n";            

The first thing you do is print the http-headers to the client. Then you print a lie that is not followed by a new-line and then you print another http-header line.

You need to print all the http-headers first and the print anything that is to make up the body of your response. Headers and body/content must be separated by an empty line.

(while we're at it, you might also use a less ancient style of calling methods)

#!c:/perl/bin/perl.exe
use strict;
use warnings;

use CGI qw/:standard/;
use CGI::Session qw/-ip-match/;
use CGI::Cookie;
use CGI::Carp qw(fatalsToBrowser);

my $session = CGI::Session->new(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 = CGI::Cookie->new(-name=>'CGISESSID',-value=>$session->id);

print "Set-Cookie: $cookie1\n";
print "Content-type: text/plain\n\n";
print "created Session\n";
于 2013-06-28T08:45:04.877 回答