无法在另一页上检索perl cgi会话

无法在另一页上检索perl cgi会话,perl,cgi,mod-perl,Perl,Cgi,Mod Perl,我是perl cgi新手。我正在使用cgi perl创建会话,并设置cookie。但我无法检索会话的值。我还粘贴了第1页和第2页的代码。谁能编辑一下代码,告诉我哪里错了。我甚至可以将会话存储在代码中提到的目录中,并且还提供了值 第1页: #!c:/perl/bin/perl.exe use strict; use CGI qw/:standard/; use

我是perl cgi新手。我正在使用cgi perl创建会话,并设置cookie。但我无法检索会话的值。我还粘贴了第1页和第2页的代码。谁能编辑一下代码,告诉我哪里错了。我甚至可以将会话存储在代码中提到的目录中,并且还提供了值

第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;                            

你在评论中说你收到了饼干。这是对的,也是错的

您的问题是第一行:

print "Content-type: text/html\n\n";                    
print "created Session";                 
...
print "Set-Cookie: $cookie1\n";            
您要做的第一件事是将http头打印到客户端。然后打印一个没有新行的谎言,然后打印另一个http头行

您需要首先打印所有http头,然后打印响应主体的任何内容。标题和正文/内容必须用空行分隔

(在我们进行此操作时,您还可以使用一种不太古老的调用方法)


你收到饼干了吗?那将是第一件要检查的事情。是的,我收到了cookie alsoNo,你没有。您在浏览器中看到cookie,但您的浏览器没有看到它。对不起,我不清楚,这使我不清楚您的答复。首先要检查的是第2页脚本是否收到cookie。您可以在第2页脚本中检查此
$sid
。我打印了$sid,并看到我的第2页正在接收cookie,但我的代码结构不正确,如innaM所述。现在我已经重新排列并得到了它。无论如何…thanx对于您的帮助,使用带有键
-type
-cookie
方法,而不是打印。thanx Inna我现在得到了它。。。现在,在编写代码时,我将更加小心:)
#!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";