我使用CGI在Perl中创建一个Web应用程序。这个应用程序实现模型视图控制器架构和系统具有在根目录下面的结构:如何在Perl中使用CGI :: Session处理Web会话?
-models -views -controllers -index.pl
文件index.pl仅包括相应根据发送到它的某些参数(使用函数参数())的观点:
这里是我的index.pl:
###############################################
# INDEX.PL
###############################################
#!/usr/bin/perl
use Switch;
use CGI qw/:standard/;
use strict;
use CGI::Session ('-ip_match');
my $session = CGI::Session->load();
print header, start_html;
print "Menu
";
if(!$session->is_empty){
#links to other files to which only logged users have access;
}
print 'Login
';
if(defined(param('p'))){
switch(param('p')){ }
##login form in html, which sends param('login') back to index.pl
case 'login' { require('views/login/login.pl'); }
else{ print "Page not found"; }
}
if(defined(param('login'))){
##if param is defined we execute login2.pl
require ('views/login/login2.pl');
}
由于你可以看到,如果链接登录访问日志中的表格将显示,则在日志中的形式提交的电子邮件地址和密码后login2.pl的文件被认为负载:
login2.pl
###############################################
LOGIN2.PL
###############################################
#!/usr/bin/perl
use CGI qw/:standard/;
use lib qw(../../);
use controllers::UserController;
use CGI::Session ('-ip_match');
my $session;
my $mail = param('mail');
my $password = param('password');
my $userc = new UserController();
my $user = $userc->findOneByMail($mail);
if($mail ne '')
{
if($mail eq $user->getEmail() and $password eq $user->getPassword())
{
$session = new CGI::Session();
$session->header(-location=>'index.exe');
}
else
{
print header(-type=>"text/html",-location=>"index.exe?p=login");
}
}
elsif(param('action') eq 'logout')
{
$session = CGI::Session->load() or die CGI::Session->errstr;
$session->delete();
print $session->header(-location=>'index.exe');
}
login2.pl文件正确执行,它应该在邮件和密码正确时创建新会话。但是,我不知道变量$ session是否正确发送到index.pl,因为索引总是只显示不需要活动会话的链接。 我的另一个问题是我无法删除会话。我试图在index.pl文件中创建一个变量$ session,以查看条件是否有效,然后我使用以下命令删除它: $ session-> delete(); $ session-> flush(); 但会议似乎仍然存在。