【问题标题】:Why doesn't CGI::Sessions work?为什么 CGI::Sessions 不起作用?
【发布时间】:2014-08-20 17:02:30
【问题描述】:

我有两个文件,一个是我从中获取用户凭据的登录名,如果他使用 CGI::Sessions 通过身份验证,我想将用户转发到另一个页面。问题是我在 hello.pl 文件中遗漏了一些东西。我在网上查找了代码示例,但找不到包含两个或更多文件的代码示例。为了使用会话重定向,我应该在 hello.pl 中的代码中添加什么,以便可以从 hello.pl 中提取变量用户名

login.pl:

 1 #!/usr/bin/perl -wT
  2  use strict;
  3 
  4 use CGI::Session;
  5 use CGI qw(:standard);
  6 
  7 my %names = map { $_ => 1 } param;
  8 my $open = "1234";
  9 
 10 sub windowLayout() {
 11           header,
 12           start_html("Input Form"),
 13           start_form,
 14           "Please enter your username:",
 15           textfield(-name=>'username',
 16                           -maxlength=>20),p,
 17           "Please enter your password:",
 18           password_field(-name=>'password',
 19                           -maxlength=>20),p,
 20           submit,hr
 21 }
 22 
 23 if ($names{username} and $names{password}) {
 24 
 25 my $cgi = new CGI;
 26 my $session = new CGI::Session(undef, $cgi, {Directory=>'/tmp'});
 27 
 28 my $cookie = $cgi->cookie(CGISESSID => $session->id);
 29 print $cgi->header(-cookie=>$cookie);
 30 
 31 my $username  = param("username");
 32 my $password  = param("password");
 33 
 34 $session->param('username', $username);
 35 $session->param('password', $password);
 36 
 37  if ($password ne $open) {
 38        print
 39         windowLayout(),
 40         end_form,
 41           p({-style=>'Color: red;'},
 42                  "Sorry wrong password!");
 43        print end_html;
 44   } else {
 45        print
 46           redirect("hello.pl");
 47   }
 48  }else {
 49     print
 50          windowLayout(),
 51          end_html;
 52  }

你好.pl

 #!/usr/bin/perl -wT
  2 use strict;
  3 
  4 use CGI::Session;
  5 use CGI qw(:standard);
  6 
  7 #my $cgi = CGI->new;
  8 #my $usrname = $cgi->param("username");
  9 
 10 my $username = $session->param("username");
 11 
 12 print   
 13     header,
 14     start_html (-BGCOLOR=>'green'),
 15     "Hello, $usrname",p,
 16     "The time now is: ",
 17      scalar(localtime),
 18 #     h1({-style=>'Color: orange;'}, "$usrname you win \$1,000,000!");
 19     end_html;

【问题讨论】:

    标签: perl redirect cgi


    【解决方案1】:

    我建议查看以下问题以获取有关使用 CGI::Session 的其他示例:

    但是,可以清理您的两个脚本以将用户名从一个转移到另一个。

    我建议的最重要的事情是首先设置CGI 对象和CGI::Session 对象。这对于您的脚本和您将创建的任何其他 cgi 样式脚本都是如此:

    login.pl

    use strict;
    use warnings;
    
    use CGI::Session;
    use CGI qw(:standard);
    
    my $open = "1234";
    
    my $cgi = CGI->new;
    my $session = CGI::Session->new( undef, $cgi, { Directory => '/tmp' } )
      or die CGI::Session->errstr;
    my $username = $cgi->param('username') // '';
    my $password = $cgi->param('password') // '';
    
    # Process Form
    my @errors;
    if ( $cgi->request_method eq 'POST' ) {
        if ( !$username ) {
            push @errors, "No username specified.";
        }
    
        if ( !$password ) {
            push @errors, "No password specified.";
        }
        elsif ( $password ne $open ) {
            push @errors, "Sorry, wrong password.";
        }
    
        if ( !@errors ) {
            $session->param( username => $username );
            print $cgi->redirect("hello.pl");
            exit;
        }
    }
    
    print $session->header(),
      start_html("Input Form"),
      start_form,
      p(
        "Please enter your username:",
        textfield(
            -name      => 'username',
            -maxlength => 20
        )
      ),
      p(
        "Please enter your password:",
        textfield(
            -name      => 'password',
            -maxlength => 20
        )
      ),
      p(submit), hr,
      end_form,
      map( { p( { -style => 'Color: red;' }, $_ ) } @errors ),
      end_html();
    

    你好.pl

    use strict;
    use warnings;
    
    use CGI::Session;
    use CGI qw(:standard);
    
    my $cgi = CGI->new;
    my $session = CGI::Session->new( undef, $cgi, { Directory => '/tmp' } )
      or die CGI::Session->errstr;
    
    my $username = $session->param('username');
    
    print header,
      start_html( -BGCOLOR => 'green' ),
      p("Hello, $username"), p( "The time now is: ", scalar(localtime) ),
      end_html;
    

    【讨论】:

    • 您显然对 Web 编程和 Perl 了解很多,所以我想知道您能否就使用哪个框架给我一些建议。我看到了标题为“为什么 Perl.pm 必须死”的视频 我不能相信那里提出的观点的有效性,因为它是由 Perl.pm 的竞争对手制作的,而且我缺乏 Perl.pm 的经验。但是,正如那里提出的那样,解决方案是 Dancer 框架。这是视频的链接。 [youtube.com/watch?v=tu6_3fZbWYw]您对此事有何看法,您会推荐哪个框架?
    • 可以在CGI::Alternatives 找到更现代的替代品列表。我对DancerMojolicious 都非常有好感,但实际上任何东西都会比CGI 更现代。它适用于介绍,但不应在其中编写新网站。
    猜你喜欢
    • 2010-11-16
    • 1970-01-01
    • 2011-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-07
    • 2019-08-06
    相关资源
    最近更新 更多