How can I redirect the client from one CGI page to

2020-07-11 09:09发布

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

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

标签: perl cgi
5条回答
看我几分像从前
2楼-- · 2020-07-11 09:21

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.

查看更多
放荡不羁爱自由
3楼-- · 2020-07-11 09:31

The easiest way is to use the META refresh tag, you wont need to regig your header either.

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
查看更多
小情绪 Triste *
4楼-- · 2020-07-11 09:34

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

I had the same problem so this trick worked for me pretty good. The code is not pretty but it works.

查看更多
冷血范
5楼-- · 2020-07-11 09:38

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.

查看更多
对你真心纯属浪费
6楼-- · 2020-07-11 09:39

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.)

查看更多
登录 后发表回答