简体   繁体   English

如何在Perl中使用CGI :: Session处理Web会话?

[英]How can I handle web sessions with CGI::Session in Perl?

Im creating a web application in Perl using CGI. 我使用CGI在Perl中创建一个Web应用程序。 This application implements the Model View Controller architecture and the system has the following structure in the root directory: 该应用程序实现了Model View Controller体系结构,并且系统在根目录中具有以下结构:

-models -views -controllers -index.pl -模型-视图-控制器-index.pl

The file index.pl only includes the respective views according to certain parameters that are sent to it (using function param() ): 文件index.pl仅根据发送给文件的某些参数(使用函数param())包括相应的视图:

Here's my 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'); }

As you can see if the link Login is accessed the log in form will show, then in the log in form after submitting the email and password the login2.pl file is supposed to load: 如您所见,如果已访问“登录名”链接,则将显示登录表单,然后在提交电子邮件和密码后在登录表单中应加载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');
  }

The login2.pl file executes correctly and it is supposed to create a new session when the mail and password are correct. login2.pl文件正确执行,并且应该在邮件和密码正确时创建一个新会话。 However, i don't know if the variable $session is correctly sent to index.pl, because the index always shows only the links which do not require an active session. 但是,我不知道变量$ session是否正确发送到index.pl,因为索引始终只显示不需要活动会话的链接。 Another problem i have is that i cannot delete a session. 我的另一个问题是我无法删除会话。 I tried creating a variable $session in the index.pl file just to see if the conditional works, and then i supposedly deleted it with the following commands: $session->delete(); 我试图在index.pl文件中创建一个变量$ session只是为了查看条件条件是否有效,然后我应该使用以下命令将其删除:$ session-> delete(); $session->flush(); $ session-> flush(); but the session seems to continue to exist. 但是该会话似乎仍然存在。

Why don't you look into catalyst ? 您为什么不研究催化剂 It's an MVC web framework for perl. 这是用于perl的MVC Web框架。 It does all the tedious Model-View-Controller coupling for you. 它为您完成了所有繁琐的Model-View-Controller耦合。 It also has many plugins, among which a Session plugin 它还有许多插件,其中有一个Session插件

Gr, ldx Gr,ldx

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM