如何使用 Perl 将客户端从一个 CGI 页面重定向到另一页面?

2024-03-14

我的问题如下。密码被识别为有效后,我需要重定向到main.cgi但我收到的消息是:

Status: 302 Found
Location: http://localhost/cgi-bin/Main.cgi

我知道这样做的原因是我在之后写下这份声明Content-Type所以它会将其作为 HTML 并打印在屏幕上。我是 Perl 的新手。任何人都可以帮我找到解决方案并使我的代码按照我想要的方式工作吗?或者请建议我一些替代代码,或者任何可能对我有帮助的链接。

#!C:\perl\bin\perl.exe
use strict;
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI;
my $q = new CGI;

print "Content-Type: text/html\n\n";

if ($q->param("Login")) {
    my $Password = param('Password');
    if (!$Password) {
        print "Please Enter the Password";
    } else {
        my $dbh = DBI->connect(
            "dbi:SQLite:DEVICE.db",
            "", "",
            {
                RaiseError => 1,
                AutoCommit => 1
            }
        );
        my $sth = $dbh->prepare("select * from Settings where Password = ?");
        $sth->execute($Password);
        if (my $pass = $sth->fetchrow_hashref) {
            print redirect(-url => 'http://localhost/cgi-bin/Main.cgi');
        } else {
            print "Invalid Password";
        }
        $dbh->disconnect;
    }
}

print <<END1;
<HTML>
    <HEAD>
        <TITLE> </TITLE>
    </HEAD>
    <body>
        <form NAME="login"  METHOD="POST">
            <input type="hidden" name="submit" value="Submit">
            <TABLE align="center" bgcolor=#B0C4DE>
                <TR>
                    <TD> Enter The Password And Click Login</TD>
                </TR>
                <TR></TR>
                <TR></TR>
                <TR></TR>
                <TR></TR>
                <TR></TR>
                <TR>
                    <TD><b>PASSWORD</b> :<input type="password" name="Password" size="20" maxlength="15" /></TD>
                </TR>
                <TR></TR>
                <TR></TR>
                <TR></TR>
                <TR></TR>
                <TR></TR>
                <TR>
                <TR>
                    <TD align="center" colspan="2">
                        <input type="submit" name="Login" value="Login">
                        <input type="reset" name="submit" value="Cancel">
                    </TD>
                </TR>
            </TABLE>
        </FORM>
   </BODY>
</HTML>
END1

重定向:

print redirect(-url=>'http://localhost/cgi-bin/Main.cgi');

仅当它是第一个发送回浏览器的内容时​​才有效。因为您首先发送的是:

print "Content-Type: text/html\n\n";

重定向被视为内容。

(重定向必须是您发送的第一个内容,因为它属于响应的 HTTP 标头。通过打印您的\n\n,您明确终止这些标头。之后,您发送的任何内容都是内容,并将由浏览器显示。)

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何使用 Perl 将客户端从一个 CGI 页面重定向到另一页面? 的相关文章

随机推荐