如何使用Perl将客户端从一个CGI页面重定向到另一页面? [英] How can I redirect the client from one CGI page to another using Perl?

查看:62
本文介绍了如何使用Perl将客户端从一个CGI页面重定向到另一页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My problem is the following. After the password is recognized as valid I need to redirect to main.cgi but I am getting the message as:

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

我知道原因是我在 Content-Type 之后编写此语句,因此将其作为HTML和在屏幕上打印。我是Perl的新手。有人可以帮我找到解决方案,并使我的代码按我想要的方式工作吗?或者,请为此提供一些替代代码或任何可能帮助我的链接。

I know the reason for this is that I am writing this statement after Content-Type so it is taking this as HTML and printing it on screen. I am a newbie to Perl. Can anybody please help me find the solution for this and make my code work the way I want it to? Or please suggest me some alternative code for this, or any link which might help me out.

#!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');

仅在第一件事发送回浏览器时起作用。因为您是第一次发送此邮件,所以:

only works when it's the first thing sent back to the browser. Because you're sending this first:

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

重定向被视为内容。

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

(The redirect has to be the first thing you send because it belongs in the HTTP headers of the response. By printing your \n\n, you're explicitly terminating those headers. After that, anything you send is content and will be displayed by the browser.)

这篇关于如何使用Perl将客户端从一个CGI页面重定向到另一页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆