CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
Help with Template Redirect

 



islanderman
Novice

Feb 26, 2014, 2:35 PM

Post #1 of 10 (14174 views)
Help with Template Redirect Can't Post

I've got a cgi script which is checking for a valid password. If its valid I want to move to another cgi script. My question is within the cgi if the password is not valid, is there a function where I can redirect to another cgi, something like

$template->redirect("to this http://....cgi")

Or does the redirect take place within the template code. The template looks like this:

<HTML>
<HEAD>
<TITLE>Testing Site</TITLE>
</HEAD>
<BODY>
<FORM ACTION="login_good.cgi" METHOD="POST">
<H3>Enter Account Information</H3>
<TABLE>
<TABLE>
<TR><TD>Enter Your Password:</TD><TD> <INPUT NAME="password" TYPE="text"></TD></TR>
</TABLE>
<INPUT TYPE="submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>


FishMonger
Veteran / Moderator

Feb 26, 2014, 4:10 PM

Post #2 of 10 (14168 views)
Re: [islanderman] Help with Template Redirect [In reply to] Can't Post

The redirection is handled in the cgi script, not the template.

I assume you're using the CGI module.
http://search.cpan.org/~markstos/CGI.pm-3.65/lib/CGI.pm#GENERATING_A_REDIRECTION_HEADER

You could handle the "redirect" with the template by deciding which template to load based on the authentication of the user's username and password. If they are authenticated, you load one template and if the authentication fails, you load a different template.


islanderman
Novice

Feb 27, 2014, 11:39 AM

Post #3 of 10 (14073 views)
Re: [FishMonger] Help with Template Redirect [In reply to] Can't Post

I am using the param method of CGI, and also using 'HTML::Template'. My main object is the Template object, so I'm not sure how to redirect this using the CGI object.

use CGI qw(param);
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;

.
.
.
$template = HTML::Template->new

checking for correct password;

if (not correct) {
redirect to some url
}

$template->param( password => $password );

print $template->html_output;


FishMonger
Veteran / Moderator

Feb 27, 2014, 11:52 AM

Post #4 of 10 (14070 views)
Re: [islanderman] Help with Template Redirect [In reply to] Can't Post


Quote
I'm not sure how to redirect this using the CGI object.

Did you read the CGI documentation that I linked to?


Code
use CGI qw(:standard); 
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;

.
.
.
$template = HTML::Template->new

checking for correct password;

if (not correct) {
redirect('http://some_url');
}

$template->param( password => $password );

print $template->html_output;


Personally, I prefer to use CGI's OO interface and you really should be using the strict pragma;

Code
use strict; 
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;

.
.
.
my $template = HTML::Template->new

my $cgi = CGI->new;

checking for correct password;

if (not correct) {
$cgi->redirect('http://some_url');
exit;
}

$template->param( password => $password );

print $template->html_output;



islanderman
Novice

Feb 27, 2014, 1:47 PM

Post #5 of 10 (14063 views)
Re: [FishMonger] Help with Template Redirect [In reply to] Can't Post

Thanks for the help. I had done exactly what you posted down below.

use strict;
use CGI;
use CGI::Carp qw(fatalsToBrowser);
use HTML::Template;

.
.
.
my $template = HTML::Template->new

my $cgi = CGI->new;

checking for correct password;

if (not correct) {
$cgi->redirect('http://some_url');
exit;
}

$template->param( password => $password );

print $template->html_output;


However on a bad password I get a script error, it's when i use 'exit;'


Script Error

The server encountered an error in your script and was unable to complete your request.


FishMonger
Veteran / Moderator

Feb 27, 2014, 1:58 PM

Post #6 of 10 (14060 views)
Re: [islanderman] Help with Template Redirect [In reply to] Can't Post

Are you printing the html header prior to the redirect statement?


FishMonger
Veteran / Moderator

Feb 28, 2014, 11:40 AM

Post #7 of 10 (13969 views)
Re: [islanderman] Help with Template Redirect [In reply to] Can't Post

Please post your responses in the open forum, not as a PM to me.

Content of PM:

Quote
Hi man, here is the full script I'm trying to test with.


#!/usr/local/bin/perl
use strict;
use warnings;

use lib qw(this is my lib);
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
use MyTemplate;
use Bank;

my $template = MyTemplate->new;
my $cgi = CGI->new;
my $account_number = param( 'account_number' );
my $input_Psw = param( 'password' );

my ($account) = Bank::Account->search( account_number => $account_number );
my $db_Psw = $account->get( 'password' );

my $salt = 'ab';
my $inputPsw = crypt $input_Psw, $salt;

my $pswgood;
if($db_Psw eq $inputPsw) {
$pswgood = 0;
}
else {
# die "\nInvalid Password\n";
my $newloc = 'atm_badpassword.cgi';
$cgi->redirect($newloc);
$pswgood = 1;
exit;
}

$template->param( account_number => $account_number,
password => $input_Psw );

print $template->html_output;


You didn't say if this version worked or not and if it failed, we would need to know the details of the failure.

I suspect that it's failing and the reason is because you're not using a full url in the redirect.

Also, the only code in the else block after the redirect statement should be the exit statement and even that isn't really needed.


FishMonger
Veteran / Moderator

Feb 28, 2014, 12:11 PM

Post #8 of 10 (13962 views)
Re: [islanderman] Help with Template Redirect [In reply to] Can't Post

New PM message:

Quote
Sorry to confuse you. I do have the full url in the redirect. I just didn't post it in the posting. But when you say that the only thing in the else block should be the exit is confusing me. I wanted to send the redirect in the else, and then exit. But this else code is failing with a script error.


You need to re-read what I posted.

How do you expect me to help you troubleshoot the problem without knowing the error?

In the future, please don't post beginning level questions like this in the advanced area.

BTW, I will not respond to any more PM's which should be posted in the open forum.


(This post was edited by FishMonger on Feb 28, 2014, 12:13 PM)


islanderman
Novice

Feb 28, 2014, 12:22 PM

Post #9 of 10 (13956 views)
Re: [FishMonger] Help with Template Redirect [In reply to] Can't Post

The error I get from the script when it hits the else block is


Script Error

The server encountered an error in your script and was unable to complete your request.

If you are the author of the CGI application we suggest you set it to log errors to the brower as follows:
Perl:
use CGI::Carp qw(fatalsToBrowser);

Php:
error_reporting(E_ALL);
ini_set("display_errors", 1);


FishMonger
Veteran / Moderator

Feb 28, 2014, 1:01 PM

Post #10 of 10 (13952 views)
Re: [islanderman] Help with Template Redirect [In reply to] Can't Post

The php stuff you posted does not have anything to do with this perl script.

The use CGI::Carp qw(fatalsToBrowser); statement will take the error message from the web server error log and output it to the browser if the script has a runtime fatal error.

Since you received the generic server error instead of the more detailed log error, that tells me that the script failed to execute.

You need to manually check the web server error log.

Is this a Windows server or a linux/unix server?

Did you edit the script directly on the server, or did you edit on a different system and upload it to the server?

What were the file permissions set to?

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives