我使用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(); 但會議似乎仍然存在。