简体   繁体   English

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

[英]How can I redirect the client from one CGI page to another using Perl?

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: 密码被识别为有效后,我需要重定向到main.cgi但我收到的消息为:

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

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. 我知道这样做的原因是我在Content-Type之后写这个语句所以它将它作为HTML并在屏幕上打印。 I am a newbie to Perl. 我是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

The redirect: 重定向:

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";

the redirect is being treated as content. 重定向被视为内容。

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

you might want to try 你可能想试试

print "<META HTTP-EQUIV=refresh CONTENT=\"1;URL=http://localhost/cgi-bin/Main.cgi\">\n";

the trick is CONTENT=\\"1 will delay page redirect for about one second 技巧是CONTENT=\\"1将延迟页面重定向约一秒钟

I had the same problem so this trick worked for me pretty good. 我有同样的问题,所以这个技巧对我很有用。 The code is not pretty but it works. 代码不漂亮,但它的工作原理。

See the following, hopefully it will give you a good idea about how to keep control flow "to the right" and will help you identify exactly which pieces do what and should do what, in your form: 请参阅以下内容,希望它能让您对如何将控制流“保持在正确的位置”提供一个好主意,并帮助您确定哪些部分可以做什么,哪些应该以您的形式执行:

#!/usr/bin/env perl
# Windows does not use #! to launch stuff!
use strict;
use warnings; # always!
use CGI qw(:standard);
use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
use DBI;

my $q = CGI->new;

my_program:
{
    if ( !$q->param('Login') or !length $q->param('Login') ) {
        print $q->header('text/html'), my_form(); # just display the form
        last my_program;
    }

    my $password = $q->param('Password');
    if ( !$password or !length $password ) {
        print $q->header('text/plain'), "Please enter the Password";
        last my_program;
    }

    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');
        last my_program;
    }
    print $q->header('text/plain'), "Invalid Password";
}

sub print_my_form {
return <<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
}

Never mind you never use the "Login" parameter... the above performs the redirection as you want it, displays the errors with no form (use a print my_form() after the header line if you need to), and looks generally a bit tidier. 不要紧,你从不使用“登录”参数...上面按你的意愿执行重定向,显示没有表格的错误(如果需要,在标题行后面使用print my_form() ),并且通常看起来像有点整洁。

To redirect a page to another use the following method. 要将页面重定向到另一个页面,请使用以下方法。

use CGI::Session;
use CGI::Session::Plugin::Redirect;
my $session = new CGI::Session();
print $session->redirect('http://example.com/redirect-path/redirect-file.php');

Search www.search.cpan.org for more details about the session module. 有关会话模块的更多详细信息,请搜索www.search.cpan.org

The easiest way is to use the META refresh tag, you wont need to regig your header either. 最简单的方法是使用META刷新标记,您也不需要重新标记标题。

Use this code: 使用此代码:

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

my $redirect = 0;

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) {
            $redirect = 1;
        } else {
            print "Invalid Password";
        }
        $dbh->disconnect;
    }
}

print <<END1;
<HTML>
    <HEAD>
END1

if ($redirect){
    print '<meta http-equiv="refresh" content="1;url=http://localhost/cgi-bin/Main.cgi/">';
}

print <<END2;
        <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>
END2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM