?
Solved

Question regarding actions....

Posted on 2005-04-07
9
Medium Priority
?
160 Views
Last Modified: 2013-12-25
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!!!
0
Comment
Question by:sousuffer
9 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 13727362
Is this what you're asking for?

    if( $login_failed ) {
        redirect_to_login_page($with_errors);
    } else {
        redirect_to_another_page( );
    }
0
 

Author Comment

by:sousuffer
ID: 13727382
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
 
LVL 18

Expert Comment

by:kandura
ID: 13727562
do you mean the actual http redirect?

    use CGI qw/:standard/;

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

0
Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

 
LVL 16

Expert Comment

by:manav_mathur
ID: 13728874
use CGI qw/:redirect/ ;
if($login_failed)
{
print redirect('/login.html') ;
} else {
print redirect ('/login_sucess_page.hml') ;
}
0
 

Author Comment

by:sousuffer
ID: 13747568
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
 
LVL 51

Expert Comment

by:ahoffmann
ID: 13748251
your script printed a header first, like
   print header();

this must not be used before redirect()
0
 

Author Comment

by:sousuffer
ID: 13762529
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
 
LVL 18

Expert Comment

by:kandura
ID: 13762668
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
 
LVL 18

Accepted Solution

by:
kandura earned 300 total points
ID: 13762714
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

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial will discuss fancy secure registration forms, with AJAX technology support. In this article I assume you already know HTML and some JS. I will write the code using WhizBase Server Pages, so you need to know some basics in WBSP (you mig…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question