How to redirect a client from one CGI page to another using Perl?

My problem is as follows. After the password is valid, I need to redirect to main.cgi , but I get a message like:

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

I know the reason is that I write this statement after the Content-Type , so it takes it as HTML and prints it on the screen. I am new to Perl. Can someone help me find a solution for this and make my code work the way I want it? Or please offer me an alternative code for this or any link that might help me.

 #!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 
+6
perl cgi
source share
5 answers

Call Forwarding:

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

only works when the first is sent back to the browser. Because you are sending this first:

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

redirection is considered as content.

(The redirect should be the first thing you send because it belongs to the HTTP response headers. By typing your \n\n , you explicitly end these headers. After that, everything you submit is content and will be displayed by the browser.)

+21
source share

you can try

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

trick CONTENT=\"1 will delay page redirection for about one second

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

+4
source share

See the following: I hope this gives you a good idea of ​​how to keep the control flow "to the right" and helps you determine exactly which parts do what and what to do 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 } 

Don’t pay attention to the fact that you never use the β€œLogin” parameter ... the above redirects as you wish, displays errors without a form (use print my_form() after the title bar if you need to), and usually looks a bit more neat.

+2
source share

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 information on the session module.

0
source share

The easiest way is to use the META update tag, you also do not need to register your title.

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 
-4
source share

All Articles