【问题标题】:Perl - Redirect loop when redirect and setting cookiePerl - 重定向和设置cookie时的重定向循环
【发布时间】:2012-05-18 03:28:36
【问题描述】:

我是一个 Perl 新手,我被这个问题困住了:

我有一个 _login.cgi 脚本,它管理登录并在凭据正确时重定向到 index.cgi 页面:

if (functions::check_credentials($input{"username"}, $input{"password"}) eq true ){

$session = new CGI::Session("driver:File", undef, {File::Spec->tmpdir});
$session->param("name", "Carcarlo Pravettoni");

$cookie = $page->cookie(CGISESSID => $session->id);
print $page->redirect( -URL => "index.cgi" -cookie=>$cookie);

} else {...}

但是当我使用正确的凭据尝试它时,我得到一个无限重定向循环到 _login.cgi(这个脚本本身)。

相反,如果我不发送带有重定向的 cookie,一切正常:

if (functions::check_credentials($input{"username"}, $input{"password"}) eq true ){

$session = new CGI::Session("driver:File", undef, {File::Spec->tmpdir});
$session->param("name", "Carcarlo Pravettoni");

$cookie = $page->cookie(CGISESSID => $session->id);
print $page->redirect( -URL => "index.cgi");

} else {...}

【问题讨论】:

    标签: perl redirect cookies cgi


    【解决方案1】:

    这里有一个错字("index.cgi" 后面缺少逗号):

    print $page->redirect( -URL => "index.cgi" -cookie=>$cookie);
    

    我建议您启用strictwarnings(可能还有diagnostics),并重构代码直到没有错误/警告。

    【讨论】:

    • 谢谢!而且...(仍然是新手)如果我启用严格和警告,我可以在 apache 错误日志中看到它们吗?
    • @qwertoyo 是的,错误/警告应该出现在错误日志中。
    【解决方案2】:
    if (functions::check_credentials($input{"username"}, $input{"password"}) eq true )
    

    如果你没有打开use strict,那么这可能是不小心做了你想做的事。

    Perl 没有布尔基本类型,因此 Perl 可能将 true 解释为字符串 'true'。您很可能在 check_credentials 函数中也犯了同样的错误,因此这两个错误相互抵消了。

    更“Perlish”的方法是check_credentials 酌情返回真或假值(可能是1undef),而if 语句不检查特定值。

    if (functions::check_credentials($input{"username"}, $input{"password"})) { ... }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-10
      • 2012-12-31
      相关资源
      最近更新 更多