【问题标题】:Passing Variable with redirect in cgi/perl在 cgi/perl 中通过重定向传递变量
【发布时间】:2014-08-30 20:52:48
【问题描述】:

我有两个 cgi 脚本。 Login.cgi 和welcome.cgi 我想在这两个页面之间传递变量 这是我的两个页面

#!C:/Perl/bin/perl.exe -w
#login.cgi
use strict;
use warnings;

use CGI::Pretty qw(:all);

my $cgi      = new CGI;
my $username = $cgi->param('username');
my $password = $cgi->param('password');

if ( ( $cgi->param('username') eq 'demo' ) && ( $cgi->param('password') eq 'demo' ) ) {
    print $cgi->redirect("welcome.cgi");
}
else {
    print header();
    print start_html( -title => "Login" );
    if ( $cgi->param('username') or $cgi->param('password') ) {
        print center( font( { -color => 'red' }, "Invalid input" ) );
    }
    print generate_form();
    print end_html();
}

sub generate_form {
    return start_form,
      h1("Please Login"),
      p( "username", textfield('username') ),
      p( "password", textfield('password') ), p(submit),
      end_form;
}

另一个页面welcome.cgi

#!C:/Perl/bin/perl.exe -w
#welcome.cgi
use warnings;
use CGI::Pretty qw(:all);
use strict;
my $cgi=new CGI;
my $username;
print header();
print start_html("Welcome"),h1("Hello, $username");
print end_html();

如何将用户名变量传递给welcome.cgi?

【问题讨论】:

  • 请将my $cgi=new CGI 替换为my $cgi = CGI->new。区别是微妙的,但是如果您养成像这样总是调用new 的习惯,那么它可能会在将来的某个时候为您节省大量的调试时间。

标签: perl redirect cgi


【解决方案1】:

使用CGI::Session

有关在脚本之间传递信息的所有不同方式以及为什么此实现是一个不错选择的教程,请阅读CGI::Session::Tutorial

在您的登录脚本中实现:

#login.cgi
use strict;
use warnings;

use CGI::Pretty qw(:all);
use CGI::Session;

my $cgi      = new CGI;
my $session  = CGI::Session->new($cgi) or die CGI->Session->errstr;
my $username = $cgi->param('username') // '';
my $password = $cgi->param('password') // '';

if ( $username eq 'demo' && $password eq 'demo' ) {
    $session->param(username => $username);
    print $cgi->redirect("welcome.cgi");
    exit;
}

print $session->header();
print start_html( -title => "Login" );
if ( $username or $cgi->param('password') ) {
    print center( font( { -color => 'red' }, "Invalid input" ) );
}
print start_form,
  h1("Please Login"),
  p( "username", textfield('username') ),
  p( "password", textfield('password') ), p(submit),
  end_form,
  end_html();

在您的欢迎脚本中:

#welcome.cgi
use strict;
use warnings;

use CGI::Pretty qw(:all);
use CGI::Session;

my $cgi      = new CGI;
my $session  = CGI::Session->new($cgi) or die CGI->Session->errstr;

my $username = $session->param('username');

print header();
print start_html("Welcome"),h1("Hello, $username");
print end_html();

【讨论】:

    【解决方案2】:

    您可以直接传递它(通过将其编码为 query string 并将其添加到您要重定向到的 URL),也可以将其存储在 cookiesession 中,然后在其他脚本。

    【讨论】:

      【解决方案3】:

      我不得不稍微不同意米勒先生的观点。 CGI::Session 目前在很多方面已经过时,尤其是由于:

      1. 设置 cookie 的方法已过时,不符合 RFC6265
      2. Cookie ID 算法不够安全,作者已转移到其他项目。

      因此除非用于教育目的,否则不应使用。

      关于原来的问题,可以看一下这个简单的教程 http://practicalperl5.blogspot.in/2014/07/perl-login-script.html

      【讨论】:

      • 问候朋友。在我的帖子中添加关于这个想法的评论会很有帮助,但幸运的是我碰巧路过。无论如何,我确信我有点同意你的想法(必须阅读更多内容)。然而,更大的想法是CGI 本身已经过时了,这是考虑到为什么它很快就会从 perl 核心中删除。因此,CGI::Session 可能仍然是使用 CGI 跟踪会话的最佳选择,但如果您知道任何更好的选择,我会很高兴。
      • 我认为最好看一下 RFC6265,了解 cookie 的真正工作原理。我的意思是“真的”。 cookie 是在登录期间设置的(如果成功),并且在成员页面上会读取它(如果不是,则将用户重定向到首页)为了帮助您,这里有一个小教程,我写了一段时间: rikacomet.blogspot.in/2014/01/… PS:由于我的rep低于50,我不能按照本站机制先发表评论。所以只好回复了。
      猜你喜欢
      • 2017-01-22
      • 2013-11-05
      • 2012-09-08
      • 2012-03-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-14
      • 1970-01-01
      相关资源
      最近更新 更多