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

son*_*nya 6 perl cgi

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

Status: 302 Found
Location: http://localhost/cgi-bin/Main.cgi
Run Code Online (Sandbox Code Playgroud)

我知道这样做的原因是我之后正在写这个语句,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
Run Code Online (Sandbox Code Playgroud)

Ric*_*dle 21

重定向:

print redirect(-url=>'http://localhost/cgi-bin/Main.cgi');
Run Code Online (Sandbox Code Playgroud)

只有在发送回浏览器的第一件事时才有效.因为你先发送这个:

print "Content-Type: text/html\n\n";
Run Code Online (Sandbox Code Playgroud)

重定向被视为内容.

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


小智 5

你可能想尝试

print "<META HTTP-EQUIV=refresh CONTENT=\"1;URL=http://localhost/cgi-bin/Main.cgi\">\n";
Run Code Online (Sandbox Code Playgroud)

技巧是CONTENT=\"1将页面重定向延迟大约一秒钟

我遇到了同样的问题,所以这个技巧对我非常有效。该代码不是很漂亮,但是可以工作。