Question regarding actions....

I have created a login page...if the login and password are incorrect, I wish to send the user back to the same page with an error message. My program currently does this. It uses an action to open the same file again with the error message as part of the perl code. I need to be able to have a clause, though, such that if the login is successful, it goes to another page. Say there is a marker called $marker1. I need to know what would be in the clause

if($marker==1)
{
     Something goes here to redirect to mypage.cgi
}

Thanks!!!
sousufferAsked:
Who is Participating?
 
kanduraCommented:
Always add "use strict; use warnings;" at the top of your script. It would have told you what was wrong straight away.
A bit of indentation doesn't hurt readability either:


    #!/usr/bin/perl -w
   
    use strict;
    use warnings;
   
    use CGI qw(:standard);
    use DBI;
    my $q=new CGI;
   
    if(param('user_name') eq "h" or param('password') eq "h")
    {
        print redirect('http://www.cnn.com');
    }
    else
    {
   
        print
            header('text/html'),
            start_html('Login/Logout page'),
            h1('Please enter your login name and password');
        print
            $q->start_form(-method=>'POST',
                    -action=>'test.cgi',
                    -enctype=>'application/x-www-form-urlencoded'),
            br,
            "Login:",
            br,
            $q->textfield(-name=>'user_name',
                    -default=>'',
                    -override=>1,
                    -size=>50,
                    -maxlength=>80),
            br,
            "Password:",
            br,
            $q->textfield(-name=>'password',
                    -default=>'',
                    -override=>1,
                    -size=>50,
                    -maxlength=>80),
            br,br,
            $q->submit(-name=>'submit_button',
                    -value=>'submit'),
            $q->endform,
   
            a({-href=>'http://bioed.bu.edu/cgi-bin/students_05/milesg/notR.cgi',-target=>'_notRegistered'},"Not yet registered? Click here"),
   
        hr;
   
        my $data_source="dbi:mysql:milesg";
        my $user="milesg";
        my $password="milesg";
   
   
        my $login=param('user_name');
        my $pass=param('password');
   
        my $dbh=DBI->connect($data_source, $user, $password) or die "error: $DBI::errstr";
        my $sth=$dbh->prepare("select l.login from logintable l where l.login='$login' and l.password='$pass'");
        my $rc=$sth->execute;
        my @array=$sth->fetchrow_array;
   
        print end_html;
    }
0
 
kanduraCommented:
Is this what you're asking for?

    if( $login_failed ) {
        redirect_to_login_page($with_errors);
    } else {
        redirect_to_another_page( );
    }
0
 
sousufferAuthor Commented:
Yes....except I don't need them the errors reported. I can do that on my own. I just need to know how to redirect exactly as you wrote it.
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
kanduraCommented:
do you mean the actual http redirect?

    use CGI qw/:standard/;

    print redirect(-url => '/login.html');

0
 
manav_mathurCommented:
use CGI qw/:redirect/ ;
if($login_failed)
{
print redirect('/login.html') ;
} else {
print redirect ('/login_sucess_page.hml') ;
}
0
 
sousufferAuthor Commented:
I tried these things with sample web pages

if ($array[0] eq $login)
{
print redirect('http://www.cnn.com');

}
else
{
print redirect('http://www.yankees.com');
}

and the only thing i got was:

Status: 302 Moved Location: http://www.cnn.com 

at the bottom of the page for a successful login and

Status: 302 Moved Location: http://www.yankees.com 

for an unsuccessful one. Any help would be appreciated.
0
 
ahoffmannCommented:
your script printed a header first, like
   print header();

this must not be used before redirect()
0
 
sousufferAuthor Commented:
here's my code....I want it so that if the user enters anything but "h" it goes to the rest of the code....otherwise with h it goes to cnn.com (as a test):


#!/usr/bin/perl -w
use CGI qw(:standard);
use DBI;
$q=new CGI;

if(param('user_name')=="h"||param('password')=="h")
{
print redirect('http://www.cnn.com');
}
else
{

print
header('text/html'),
start_html('Login/Logout page'),
h1('Please enter your login name and password');
print
$q->start_form(-method=>'POST',
-action=>'test.cgi',
-enctype=>'application/x-www-form-urlencoded'),
br,
"Login:",
br,
$q->textfield(-name=>'user_name',
-default=>'',
-override=>1,
-size=>50,
-maxlength=>80),
br,
"Password:",
br,
$q->textfield(-name=>'password',
-default=>'',
-override=>1,
-size=>50,
-maxlength=>80),
br,br,
$q->submit(-name=>'submit_button',
-value=>'submit'),
$q->endform,

a({-href=>'http://bioed.bu.edu/cgi-bin/students_05/milesg/notR.cgi',-target=>'_notRegistered'},"Not yet registered? Click here"),

hr;

my $data_source="dbi:mysql:milesg";
my $user="milesg";
my $password="milesg";


my $login=param('user_name');
my $pass=param('password');

my $dbh=DBI->connect($data_source, $user, $password) or die "error";
my $sth=$dbh->prepare("select l.login from logintable l where l.login='$login' and l.password='$pass'");
my $rc=$sth->execute;
my @array=$sth->fetchrow_array;

}
end_html;
0
 
kanduraCommented:
sousuffer,
> if(param('user_name')=="h"||param('password')=="h")

You're comparing strings, not numbers:

if( param('user_name') eq "h" or param('password') eq "h" )
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.