Solved

Perl Images/Redirection and Javascript

Posted on 2007-11-28
19
464 Views
Last Modified: 2013-12-25
I have this script below. I'm new to perl so this is pretty basic. Its a simple login

#!\xampp\perl\bin\perl.exe
# Academics are us Admin Login

use CGI;

$upload_form = new CGI;
print $upload_form->header,
        $upload_form->start_html('Login');

print $upload_form->start_form();

## for testing purpose
print $upload_form->param, $upload_form->p();
print $upload_form->param_fetch('username')->[0],
                                 $upload_form->p();
print $upload_form->param_fetch('passwd')->[0],
                                 $upload_form->p();


$upload_form->import_names('upload_form_names');

if (!$upload_form_names::username or
            !$upload_form_names::passwd) {
    printLogin();
    }
elsif ($upload_form_names::username eq 'User'
        and $upload_form_names::passwd  eq 'password') {
    # The below line is where you would have this script
    # set a cookie and forward the user to the admin area.
    print 'Authorised Login';  }
else {
    # The below line where you would put
    # whatever you want for bad user/pass
    # combinations.
    print 'unAuthorised Login'; }

print $upload_form->end_html;

sub printLogin
{
    print 'username: ',
        $upload_form->textfield({-name=>'username',
               # -default=>'nunya',
                -size=>20,
                -maxlength=>20});

    print $upload_form->p();

    print 'password ',
        $upload_form->password_field({-name=>'passwd',
                #-value=>'nunya',
                -size=>20,
                -maxlength=>20});
    print $upload_form->p();
    print $upload_form->submit({-name=>'login_btn',
                -value=>'Login'});

    print $upload_form->end_form();

    print $upload_form->end_html;
}


My first question is. I have an image on the cgi folder within the server called 'adminLogin.jpg'. I tried lots of different ways of adding this image and looked at a few solutions throughout the web but I still wasn't able to get it working. Could anyone tell me the line I am supposed to add in

Second question is. I want the user to be redirected to 'adminLogin.pl' if he or she enters in the correct user name and password

Lastly. Is it possible to use javascript within perl
0
Comment
Question by:bigbong00
  • 9
  • 8
  • 2
19 Comments
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
1)  The cgi-bin directory is where your scripts are placed, not .jpg files.  Move your images to a folder under your public_html directory (or whatever name is being used by the web server).

2)  You need to hold off printing any output until you've verified the username/password.  If authentication fails, then do a redirect to the login page.
http://search.cpan.org/~lds/CGI.pm-3.29/CGI.pm#GENERATING_A_REDIRECTION_HEADER

3)  Yes, you can output javascript code with Perl.  If you want it in the head section, you'd use the -script attribute in the start_html() method, otherwise you'd output it afterwards.

The documentation for the CGI module gives examples on how to handle each of your questions.
0
 

Author Comment

by:bigbong00
Comment Utility
So i added in the image like so but I'm still getting the error
0
 

Author Comment

by:bigbong00
Comment Utility
#!\xampp\perl\bin\perl.exe
# Academics are us Admin Login

use CGI;

print <html><img src="http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"></html>;

$upload_form = new CGI;
print $upload_form->header,
        $upload_form->start_html('Login');

print $upload_form->start_form();

## for testing purpose
print $upload_form->param, $upload_form->p();
print $upload_form->param_fetch('username')->[0],
                                 $upload_form->p();
print $upload_form->param_fetch('passwd')->[0],
                                 $upload_form->p();

$upload_form->import_names('upload_form_names');

if (!$upload_form_names::username or
            !$upload_form_names::passwd) {
    printLogin();
    }
elsif ($upload_form_names::username eq 'User'
        and $upload_form_names::passwd  eq 'password') {
    # The below line is where you would have this script
    # set a cookie and forward the user to the admin area.
    print 'Authorised Login';  }
else {
    # The below line where you would put
    # whatever you want for bad user/pass
    # combinations.
    print 'unAuthorised Login'; }

print $upload_form->end_html;

sub printLogin
{
    print 'username: ',
        $upload_form->textfield({-name=>'username',
               # -default=>'nunya',
                -size=>20,
                -maxlength=>20});

    print $upload_form->p();

    print 'password ',
        $upload_form->password_field({-name=>'passwd',
                #-value=>'nunya',
                -size=>20,
                -maxlength=>20});
    print $upload_form->p();
    print $upload_form->submit({-name=>'login_btn',
                -value=>'Login'});

    print $upload_form->end_form();

    print $upload_form->end_html;
}

I've also looked through the submit re-direct thing. Can't pick anyhting out to help me
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> print <html><img src="http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"></html>;
please remove this line 'cause it breaks your html structure
Use something like:
 print start_html(), img(-src=>"http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg");

# where I highliy recommend that you omiz the http:... part and just use the path on the webserver, also keep the suggestion about the images directory in mind

> Second question is. ...
depending on your logic use this code to redirect the user

print CGI::header(-location=>"/adminLogin.pl");


> Is it possible to use javascript within perl
if you mean that perl writes javascript as output to the client, yes
if you mean that you write javascript code which is used/done by perl, no

0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
I've made a few adjustments to get you started, but there are number of things that could/should be fixed and/or better implemented.
use CGI::Carp qw(fatalsToBrowser);

use CGI;
 
 

$upload_form = new CGI;
 
 

## for testing purpose

#print $upload_form->param, $upload_form->p();

#print $upload_form->param_fetch('username')->[0],

#                                 $upload_form->p();

#print $upload_form->param_fetch('passwd')->[0],

#                                 $upload_form->p();
 

$upload_form->import_names('upload_form_names');
 

if (!$upload_form_names::username or

            !$upload_form_names::passwd) {

    printLogin();

    }

elsif ($upload_form_names::username eq 'User'

        and $upload_form_names::passwd  eq 'password') {

    # The below line is where you would have this script

    # set a cookie and forward the user to the admin area.

    print $upload_form->header,

          $upload_form->start_html('Authorized Login');

    print 'Authorised Login';  }

else {

    # The below line where you would put

    # whatever you want for bad user/pass

    # combinations.
 

 # this is only 1 of several methods to redirect

    print $upload_form->redirect($upload_form->url()); }
 

print $upload_form->end_html;
 

sub printLogin

{

    print $upload_form->header,

          $upload_form->start_html('Login');

    print $upload_form->img({src=>"http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"});

    print $upload_form->start_form();

    print 'username: ',

        $upload_form->textfield({-name=>'username',

               # -default=>'nunya',

                -size=>20,

                -maxlength=>20});
 

    print $upload_form->p();
 

    print 'password ',

        $upload_form->password_field({-name=>'passwd',

                #-value=>'nunya',

                -size=>20,

                -maxlength=>20});

    print $upload_form->p();

    print $upload_form->submit({-name=>'login_btn',

                -value=>'Login'});
 

    print $upload_form->end_form();
 

}

Open in new window

0
 

Author Comment

by:bigbong00
Comment Utility
Sorry it has taken me so long to reply. I'm havinbg problems with these lines you have created
I don't want to redirect the user anywhere if he enters in a bad password. Just would like some javascript (which I plan to do later) but he should be redirected when the password and username match

elsif ($upload_form_names::username eq 'User'
        and $upload_form_names::passwd  eq 'password') {
    # The below line is where you would have this script                   ###### how do i do this?
    # set a cookie and forward the user to the admin area.
    print $upload_form->header,
          $upload_form->start_html('Authorized Login');
    print 'Authorised Login';  }
else {
    # The below line where you would put
    # whatever you want for bad user/pass                        #########Don't want to redirect user here
    # combinations.
 
 # this is only 1 of several methods to redirect
    print $upload_form->redirect($upload_form->url()); }
 
print $upload_form->end_html;
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
Never use javascript for username/password verification!  Why, you ask?  Because that requires you to provide the username and password in the javascript and all the user has to do is "view source" to see what username/password they need to enter.  Javascript can be used as a preliminary check to see if the username and/or password is correctly formatted.  However, since users can disable javascript, you should never rely on those tests.  All authorization checks should be handled by the perl script.

If the authentication fails, you need to redirect back to the login page.

Creating and sending the cookie could be handled with either the CGI or CGI::Cookie module.
http://search.cpan.org/~lds/CGI.pm-3.31/CGI.pm#HTTP_COOKIES
http://search.cpan.org/~lds/CGI.pm-3.31/CGI/Cookie.pm

Another option, which I think is better, is to use CGI::Session.
http://search.cpan.org/~markstos/CGI-Session-4.20/lib/CGI/Session.pm
0
 

Author Comment

by:bigbong00
Comment Utility
I think you misunderstood me. When the user fails to enter the correct username and password some red writting will appear above the input box to say there has been an error. Thats all. I don't plan to validate anything with javascript. I've read through all the cookie information you sent

expiration time
name
value
path
domain
secure

I take it I am supposed to send this information but what info do i send? How do I send it. Who do I send it to? I'm completely new to perl and fairly new to website design
0
 
LVL 51

Expert Comment

by:ahoffmann
Comment Utility
> When the user fails to enter the correct  ..
you have to send the entered data to the server (your perl/CGI script) which then needs to return the same page with the additional info you want to be shown.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
It appears that you're having a hard time understanding the documentation that I pointed to as well as the overall process that you need to accomplish.

Rather than cleaning-up and fixing your script, I'll post one of my login scripts that I've adjusted to include the javascript that you asked about.  I'm pulling my user info from a database, but the combination of this script and the documentation should be enough for you to figure out how to modify it to your needs.

I'm also hoping that I'm not providing you with the answer to a homework assignment.
#!/usr/bin/perl
 

use strict;

use CGI;

use CGI::Carp qw(fatalsToBrowser);

use CGI::Session;

use Crypt::PasswdMD5;

use DBI;
 

my $hostname = `hostname`;

my $cgi = new CGI;

my $self = $cgi->url;

my %login = $cgi->Vars;

my $session = new CGI::Session or die CGI::Session->errstr;

my $title = 'Email Administration Login';

my $alert;
 

my $css = <<CSS;

body {

      background-image: url(/images/left-logo.gif);

      background-repeat: no-repeat;

      background-position: 5px 5px;

      background-color: #cccccc;

      font-family: times;

      text-align: center;

      font-size: small

     }
 

.host {

         position: absolute;

         z-index: auto;

         top: 30px;

         left: 10px;

         font-size: 8;

        }
 

CSS
 
 

if ( $cgi->param('logout') ) {

   $session->clear(['admin', 'logged_in']);

}
 

if( $cgi->param('Login') ) {
 

   my $home_page = 'http://mydomain.com/cgi-bin/home_page.pl';

   my $authenticated = authenticate_user();

   print $cgi->redirect($home) if $authenticated;

}
 

login_page();
 
 

sub login_page {
 

   print $session->header;
 

   if($alert) {

      print $cgi->start_html(-title=>$title,

                             -style=>{-code=>"$css"},

                             -script=>{-type=>'text/javascript',

                                       -code=>$alert},

                             -onLoad=>'failed();'

                            );

   }

   else {

      print $cgi->start_html(-title=>$title,

                             -style=>{-code=>"$css"}

                            );

   }
 

   print $cgi->h1($title),

         $cgi->start_form(-name=>'login'),

         $cgi->p('Username: ', $cgi->textfield('username')),

         $cgi->p('Password: ', $cgi->password_field('password', '')),

         $cgi->p($cgi->submit('Login', 'Login')),,

         $cgi->end_form;
 

   print $login{'failed'} if defined $login{'failed'};

   print $cgi->end_html;

}
 
 

sub authenticate_user {
 

   if ( defined $login{'username'} && defined $login{'password'} ) {
 

      my ($encrypted_pass, $roll, $name) = queryDB($login{'username'});
 

      if ( $encrypted_pass ) {
 

         my $salt = substr($encrypted_pass, 3,8);

         my $password = unix_md5_crypt( $login{'password'}, $salt );
 

         if ( $password eq $encrypted_pass ) {

            $session->param('logged_in', 1);

            $session->param('user', $login{'username'});

            $session->param('admin', $name);

            $session->param('roll', $roll);

            return 1;

         }

      }

   }

   $login{'failed'} = 'Invalid username, or password...Please try again';

   $alert = qq/function failed() { alert("$login{'failed'}"); }/;

   return 0;

}
 
 

sub queryDB {
 

   my $user = shift;

   my %db = (

             server   => 'dbserver.mydomain.com',

             name     => 'users',

             username => 'username',

             password => 'password'

            );
 
 

   my $dbh = DBI->connect("DBI:mysql:$db{'name'}:$db{'server'}",

                           $db{'username'}, $db{'password'},

                         {'RaiseError' => 1, 'PrintError' => 0 })

                  or die "Connection Failed: $iso_db{'name'} DB on " .

                         "$db{'server'}\n\t$DBI::errstr\n";
 

   my $sth = $dbh->prepare("SELECT password, roll, name

                            FROM users

                            WHERE id = '$user' and status = 'active' limit 1")

                   or die "prepare statemnet failed: $DBI::errstr\n";

   $sth->execute;

   my ($password, $roll, $name) = $sth->fetchrow_array;
 

   $sth->finish;

   $dbh->disconnect;

   return ($password, $roll, $name);

}

Open in new window

0
 

Author Comment

by:bigbong00
Comment Utility
My server seems to be down. I'll check that out.

Yeah it was understanding perl in general I was having difficulty with. Never done it defore so I've just been looking at examples throihjout the web.

Thanks
0
 

Author Comment

by:bigbong00
Comment Utility
Still having problems. It seems to have problems with lines 102 and 107 which are my authentication. I'm not using a database

#!\xampp\perl\bin\perl.exe
# Academics are us Admin Login

use CGI::Carp qw(fatalsToBrowser);
use CGI;

my $cgi = new CGI;
my $self = $cgi->url;
my %login = $cgi->Vars;
my $session = new CGI::Session or die CGI::Session->errstr;
my $alert;
 
$cgi->import_names('cgi_names');
 
my $css = <<CSS;
body {

      background-color:white;
      font-family:arial;
      text-align: center;
      font-size: small
     }
 
.host {
         position: absolute;
         z-index: auto;
         top: 30px;
         left: 10px;
         font-size: 8;
        }
 
CSS
 
 
if ( $cgi->param('logout') ) {
   $session->clear(['admin', 'logged_in']);
}
 
if( $cgi->param('Login') ) {
 
   my $admin_page = 'http://php.infc.ulst.ac.uk/cgi-bin/com851c1/tis07sa6/AdminArea.pl';
   my $authenticated = authenticate_user();
   print $cgi->redirect($home) if $authenticated;
}
 
login_page();
 
sub printLogin
{
   print $session->header;
 
   if($alert) {
      print $cgi->start_html(-title=>$title,
                             -style=>{-code=>"$css"},
                             -script=>{-type=>'text/javascript',
                                       -code=>$alert},
                             -onLoad=>'failed();'
                            );
   }
   else {
      print $cgi->start_html(-title=>$title,
                             -style=>{-code=>"$css"}
                            );
   }
   
    print $cgi->header,
          $cgi->start_html('Login');
    print $cgi->img({src=>"http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"});
    print $cgi->start_form();
    print 'username: ',
        $cgi->textfield({-name=>'username',
               # -default=>'nunya',
                -size=>20,
                -maxlength=>20});
 
    print $cgi->p();
 
    print 'password ',
        $cgi->password_field({-name=>'passwd',
                #-value=>'nunya',
                -size=>20,
                -maxlength=>20});
    print $cgi->p();
    print $cgi->submit({-name=>'login_btn',
                -value=>'Login'});
 
    print $cgi->end_form();
      
         print $login{'failed'} if defined $login{'failed'};
   print $cgi->end_html;
 
}

sub authenticate_user {
 
if (!$cgi_names::username or
            !$cgi_names::passwd) {
    printLogin();
    }
else ($cgi_names::username eq 'User'
        and $cgi_names::passwd  eq 'password') {

    print 'Authorised Login';  }
            return 1;
         }
   $login{'failed'} = 'Invalid username, or password...Please try again';
   $alert = qq/function failed() { alert("$login{'failed'}"); }/;
   return 0;
}
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
Change line 43 from:
print $cgi->redirect($home) if $authenticated;

To:
print $cgi->redirect($admin_page) if $authenticated;

The else clause on line 100 is missing the opening brace and 'if' keyword, change it from:
else ($cgi_names::username eq 'User'

to:
else {
    if ($cgi_names::username eq 'User'
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
Change:
$cgi_names::username

to:
$login{'username'}

and change:
$cgi_names::password

to:
$login{'password'}

Add the strict pragma and you'll find a couple more items that need to be fixed.
use strict;
0
 

Author Comment

by:bigbong00
Comment Utility
THANKS MR FISHMONGER THOUGH I AM NOW GETTING THE ERROR

Can't locate object method "new" via package "CGI::Session" (perhaps you forgot to load "CGI::Session"?) at D:/xampp/htdocs/com851c1/tis07sa6/login.pl line 10.

I'VE LOOKED AT SOME FIX ITS THROUGHOUT THE WEB AND I AM NOT UNDERSTANDING THE QUESTION TOO WELL. LIKE THIS ONE BELOW

http://www.dbforums.com/archive/index.php/t-766555.html
0
 
LVL 28

Expert Comment

by:FishMonger
Comment Utility
You're receiving the error because you removed the line that loads the CGI::Session module.
use CGI::Session;

Depending on your requirements, you may not need that that module.  If you don't need/want to use that module, then make the following changes.

1) remove this line:
my $session = new CGI::Session or die CGI::Session->errstr;

2) change:
print $session->header;

to:
print $cgi->header;
0
 

Author Comment

by:bigbong00
Comment Utility
sorry it has taken me so long to write back. Still getting the same error message

#!\xampp\perl\bin\perl.exe
# Academics are us Admin Login

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

my $cgi = new CGI;
my $self = $cgi->url;
my %login = $cgi->Vars;
my $alert;
 
$cgi->import_names('cgi_names');
 
my $css = <<CSS;
body {

      background-color:white;
      font-family:arial;
      text-align: center;
      font-size: small
     }
 
.host {
         position: absolute;
         z-index: auto;
         top: 30px;
         left: 10px;
         font-size: 8;
        }
 
CSS
 
 
if ( $cgi->param('logout') ) {
   $session->clear(['admin', 'logged_in']);
}
 
if( $cgi->param('Login') ) {
 
   my $admin_page = 'http://php.infc.ulst.ac.uk/cgi-bin/com851c1/tis07sa6/AdminArea.pl';
   my $authenticated = authenticate_user();
   print $cgi->redirect($home) if $authenticated;
}
 
login_page();
 
sub printLogin
{
   print $cgi->header;
 
   if($alert) {
      print $cgi->start_html(-title=>$title,
                             -style=>{-code=>"$css"},
                             -script=>{-type=>'text/javascript',
                                       -code=>$alert},
                             -onLoad=>'failed();'
                            );
   }
   else {
      print $cgi->start_html(-title=>$title,
                             -style=>{-code=>"$css"}
                            );
   }
   
    print $cgi->header,
          $cgi->start_html('Login');
    print $cgi->img({src=>"http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"});
    print $cgi->start_form();
    print 'username: ',
        $cgi->textfield({-name=>'username',
               # -default=>'nunya',
                -size=>20,
                -maxlength=>20});
 
    print $cgi->p();
 
    print 'password ',
        $cgi->password_field({-name=>'passwd',
                #-value=>'nunya',
                -size=>20,
                -maxlength=>20});
    print $cgi->p();
    print $cgi->submit({-name=>'login_btn',
                -value=>'Login'});
 
    print $cgi->end_form();
     
         print $login{'failed'} if defined $login{'failed'};
   print $cgi->end_html;
 
}

sub authenticate_user {
 
if (!$login{'username'} or
            $login{'password'}) {
    printLogin();
    }
else{
      if ($login{'username'}eq 'user'
        and $login{'password'}  eq 'password') {

    print 'Authorised Login';  }
            return 1;
         }
   $login{'failed'} = 'Invalid username, or password...Please try again';
   $alert = qq/function failed() { alert("$login{'failed'}"); }/;
   return 0;
}
0
 
LVL 28

Accepted Solution

by:
FishMonger earned 500 total points
Comment Utility
I've added some comments to you code that point out most of the problems and I'm including a cleaned up version at the end.

#!\xampp\perl\bin\perl.exe
# Academics are us Admin Login

use CGI::Carp qw(fatalsToBrowser);
use CGI;
use strict;
use CGI::Carp qw(fatalsToBrowser);  # no reason to attempt to load this module twice

my $cgi = new CGI;
my $self = $cgi->url;
my %login = $cgi->Vars;
my $alert;

# no reason to do this since we've already imported the form submission into the %login hash
$cgi->import_names('cgi_names');
 
my $css = <<CSS;
body {

      background-color:white;
      font-family:arial;
      text-align: center;
      font-size: small
     }
 
.host {
         position: absolute;
         z-index: auto;
         top: 30px;
         left: 10px;
         font-size: 8;
        }
 
CSS
 
# since you're not using the CGI::Session module, you'll need to take this out
if ( $cgi->param('logout') ) {
   $session->clear(['admin', 'logged_in']);
}

# make sure that the 'Login' param matches the -name attribute, not the -value
if( $cgi->param('Login') ) {
 
   my $admin_page = 'http://php.infc.ulst.ac.uk/cgi-bin/com851c1/tis07sa6/AdminArea.pl';
   my $authenticated = authenticate_user();
   
   # you're redirecting to $home which isn't defined
   # you should be redirecting to $admin_page
   print $cgi->redirect($home) if $authenticated;
}

# this sub isn't defined, you meant printLogin()
login_page();
 
sub printLogin
{
   print $cgi->header;
 
   if($alert) {
   
                        # $title has not been defined
      print $cgi->start_html(-title=>$title,
                             -style=>{-code=>"$css"},
                             -script=>{-type=>'text/javascript',
                                       -code=>$alert},
                             -onLoad=>'failed();'
                            );
   }
   else {
      print $cgi->start_html(-title=>$title,
                             -style=>{-code=>"$css"}
                            );
   }
   
   # you've aready output the header, no reason to do it again.
    print $cgi->header,
          $cgi->start_html('Login');
    print $cgi->img({src=>"http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"});
    print $cgi->start_form();
    print 'username: ',
        $cgi->textfield({-name=>'username',
               # -default=>'nunya',
                -size=>20,
                -maxlength=>20});
 
    print $cgi->p();
 
    print 'password ',
        $cgi->password_field({-name=>'passwd',
                #-value=>'nunya',
                -size=>20,
                -maxlength=>20});
    print $cgi->p();
   
    # the -name attribute needs to match what you are checking for at the begining
    print $cgi->submit({-name=>'login_btn',
                -value=>'Login'});
 
    print $cgi->end_form();
     
         print $login{'failed'} if defined $login{'failed'};
   print $cgi->end_html;
 
}

sub authenticate_user {

# this conditional will run the printLogin() sub if the user enters a password
if (!$login{'username'} or
            $login{'password'}) {
    printLogin();
    }
else{
      if ($login{'username'}eq 'user'
        and $login{'password'}  eq 'password') {

    print 'Authorised Login';  }  # remove this extra closing brace
            return 1;
         }
   $login{'failed'} = 'Invalid username, or password...Please try again';
   $alert = qq/function failed() { alert("$login{'failed'}"); }/;
   return 0;
}

#### script end ####

I've removed the CGI::Session related portion to make it a little easier for you.  However, it would be better to use session vars for maintaining state.
#!\xampp\perl\bin\perl.exe

# Academics are us Admin Login
 

use CGI;

use strict;

use CGI::Carp qw(fatalsToBrowser);
 

my $cgi = new CGI;

my $self = $cgi->url;

my %login = $cgi->Vars;

my $alert;
 
 

my $css = <<CSS;

body {

      background-color:white;

      font-family:arial;

      text-align: center;

      font-size: small

     }
 

.host {

      position: absolute;

      z-index: auto;

      top: 30px;

      left: 10px;

      font-size: 8;

      }

CSS
 
 

if( $cgi->param('login_btn') ) {
 

   my $admin_page = 'http://php.infc.ulst.ac.uk/cgi-bin/com851c1/tis07sa6/AdminArea.pl';

   my $authenticated = authenticate_user();

   print $cgi->redirect($admin_page) if $authenticated;

}
 

login_page();
 
 

sub login_page

{

   my $title = 'Login';

   print $cgi->header;
 

   if($alert) {

      print $cgi->start_html(-title  => $title,

                             -style  => {-code=>$css},

                             -script => {-type=>'text/javascript', -code=>$alert},

                             -onLoad => 'failed();'

                            );

   }

   else {

      print $cgi->start_html(-title => $title, -style => {-code=>"$css"});

   }
 

    print $cgi->img({-src => "http://php.infc.ulst.ac.uk/com851c1/tis07sa6/adminLogin.jpg"});

    print $cgi->start_form(-action => $self),

          'username: ',

          $cgi->textfield({-name      => 'username',

                           -size      => 20,

                           -maxlength => 20});
 

    print $cgi->p(),

          'password ',

          $cgi->password_field({-name=>'password',

                                -size=>20,

                                -maxlength=>20});

    print $cgi->p(),

          $cgi->submit({-name=>'login_btn', -value=>'Login'}),

          $cgi->end_form();
 

    print $cgi->p($login{'failed'}) if defined $login{'failed'};
 

    print $cgi->end_html;
 

}
 

sub authenticate_user {
 

   if ($login{'username'} && $login{'password'}) {

      if ($login{'username'}eq 'user' && $login{'password'}  eq 'password') {

         return 1;

      }

   }

   $login{'failed'} = 'Invalid username, or password...Please try again';

   $alert = qq/function failed() { alert("$login{'failed'}"); }/;

   return 0;

}

Open in new window

0
 

Author Comment

by:bigbong00
Comment Utility
It works. I'm well pleased. Thanks also for writting out the errors
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

This article will show, step by step, how to integrate R code into a R Sweave document
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

744 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now