Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    login form using perl cgi script

    Hi frds, i am new in PERL CGI scripting.i want to create a login form, on which user enter username and password and click on submit.If username and password available in database table,then page will be redirect on next page otherwise its showing the same login page with error....i have write a code but i dont know how to redirect the page and how to call the submit button...please help me :-
    This is a my code :-userlogin.cgi
    Code:
    #!/usr/bin/perl
    use CGI qw(:standard);
    use CGI::Pretty qw(:all);
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use DBI;
    use DBD::mysql;
    use DBI qw(:sql_types);
    my $query = new CGI;
    
    my $error;
    
    use CGI qw/:standard/;
    
    
    print "Content-type: text/html\n\n";
    print "<html><h1>OTT Login</h1></html>\n";
    print '<body bgcolor="gray">';
    
    
    #start a form----------------
    
    print '<form method=POST action="">';
    
    
    print '<p>';
    print 'Employee Name: <p><INPUT type="text" name="User" size=25 maxlength=25></p>';
    print '</p>';
    
    # Create a text box for Password:---------------
    
    print '<p>';
    print 'Password:<p><INPUT TYPE=PASSWORD NAME="mypassword" id = "Password" size = "15" maxlength = "15" tabindex = "1"/></p>';
    print '</p>';
    
    #Create submit & reset button:-------------------
    
    #print '<p><input type=" button" name="submitit"value="submit"onclick="formvalidation(myform)"/></p>';
    print '<form name="input" action="ott/menu.cgi" method="post">';
    print '<p><input type="submit" value="Submit" /><INPUT TYPE="reset" name = "Reset" value = "Reset"></p>';
    
    #Create Change Password & Reset Password link:------------
    print '<p><a href="changepwd.cgi">Change Password</a></p>';
    print '<p><a href="userlogin.cgi">Reset Password</a></p>';
    print '</form>';
    
    #logic for submit button functionality :-----------------
    
    
    if (param('User') and param('mypassword'))
    {
    $usr=ucfirst(lc(param('User')));
    $pwd=ucfirst(lc(param('mypassword')));
    print start_html("OTT Login"),h1("User name: $usr\n"), h1("Password: $pwd\n");
    
    #my $url = "menu.cgi";
        		my $driver = "mysql";
        		my $database = "testottdb";
        		my $user = "ott"; 
       		my $password = "perl123";
        		my $dbh = DBI->connect("DBI:$driver:$database",$user, $password,) or die "$DBI::errstr\n";
                    $sth=$dbh->prepare("Select emp_id from employee where emp_name='$usr'") || die "$DBI::errstr\n";
    		$sth->execute() || die "$DBI::errstr\n";
    		#$x=$sth->fetchrow();
    		print "$x\n";	
    			
                    $sth1 = $dbh->prepare("Select emp_password from employee where emp_password='$pwd'") or &dbdie;
                    $sth1->execute() || die "$DBI::errstr\n";
                    #$y=$sth1->fetchrow_array;
    		print "$y\n";
    		if ($x=$sth->fetchrow())
                    {
                     if ($y=$sth1->fetchrow())
                     {
                       print $cgi->redirect("ott/menu.cgi");
                        #print "Location:ott/menu.cgi\n\n";
    
                      #print redirect($url);
                     print "Correct Password";
                     }
                        else
                       {
                       print "Incorrect Password";
                          }
    		}
    		
                  else
                    {
                   print "Incorrect Emp_Id";
                    }
    
    		$dbh->disconnect || die "$DBI::errstr\n";
    }
    Last edited by FishMonger; 02-18-2011 at 07:29 PM. Reason: Added code tags

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by ripusuden View Post
    Hi frds, i am new in PERL CGI scripting.i want to create a login form, on which user enter username and password and click on submit.If username and password available in database table,then page will be redirect on next page otherwise its showing the same login page with error....i have write a code but i dont know how to redirect the page and how to call the submit button...please help me :-
    This is a my code :-userlogin.cgi

    Code:
    #!/usr/bin/perl
    use CGI qw(:standard);
    use CGI::Pretty qw(:all);
    use CGI::Carp qw(warningsToBrowser fatalsToBrowser);
    use DBI;
    use DBD::mysql;
    use DBI qw(:sql_types);
    my $query = new CGI;
    
    my $error;
    
    use CGI qw/:standard/;
    
    
    print "Content-type: text/html\n\n";
    print "<html><h1>OTT Login</h1></html>\n";
    print '<body bgcolor="gray">';
    
    
    #start a form----------------
    
    print '<form method=POST action="">';
    
    
    print '<p>';
    print 'Employee Name: <p><INPUT type="text" name="User" size=25 maxlength=25></p>';
    print '</p>';
    
    # Create a text box for Password:---------------
    
    print '<p>';
    print 'Password:<p><INPUT TYPE=PASSWORD NAME="mypassword" id = "Password" size = "15" maxlength = "15" tabindex = "1"/></p>';
    print '</p>';
    
    #Create submit & reset button:-------------------
    
    #print '<p><input type=" button" name="submitit"value="submit"onclick="formvalidation(myform)"/></p>';
    print '<form name="input" action="ott/menu.cgi" method="post">';
    print '<p><input type="submit" value="Submit" /><INPUT TYPE="reset" name = "Reset" value = "Reset"></p>';
    
    #Create Change Password & Reset Password link:------------
    print '<p><a href="changepwd.cgi">Change Password</a></p>';
    print '<p><a href="userlogin.cgi">Reset Password</a></p>';
    print '</form>';
    
    #logic for submit button functionality :-----------------
    
    
    if (param('User') and param('mypassword'))
    {
    $usr=ucfirst(lc(param('User')));
    $pwd=ucfirst(lc(param('mypassword')));
    print start_html("OTT Login"),h1("User name: $usr\n"), h1("Password: $pwd\n");
    
    #my $url = "menu.cgi";
        		my $driver = "mysql";
        		my $database = "testottdb";
        		my $user = "ott"; 
       		my $password = "perl123";
        		my $dbh = DBI->connect("DBI:$driver:$database",$user, $password,) or die "$DBI::errstr\n";
                    $sth=$dbh->prepare("Select emp_id from employee where emp_name='$usr'") || die "$DBI::errstr\n";
    		$sth->execute() || die "$DBI::errstr\n";
    		#$x=$sth->fetchrow();
    		print "$x\n";	
    			
                    $sth1 = $dbh->prepare("Select emp_password from employee where emp_password='$pwd'") or &dbdie;
                    $sth1->execute() || die "$DBI::errstr\n";
                    #$y=$sth1->fetchrow_array;
    		print "$y\n";
    		if ($x=$sth->fetchrow())
                    {
                     if ($y=$sth1->fetchrow())
                     {
                       print $cgi->redirect("ott/menu.cgi");
                        #print "Location:ott/menu.cgi\n\n";
    
                      #print redirect($url);
                     print "Correct Password";
                     }
                        else
                       {
                       print "Incorrect Password";
                          }
    		}
    		
                  else
                    {
                   print "Incorrect Emp_Id";
                    }
    
    		$dbh->disconnect || die "$DBI::errstr\n";
    
    }
    welcome to cf,

    put your code between [ code] and [ /code] tags please( without spaces), is more easy to read, you can edit your post to do this, thank you.

    if you use cgi module you can avoid to print explicit html by using the module, see the perldoc documentation or the man page for cgi.
    put the url to the script or relative path in the action attribute of the form element that will do what you want, but remove the onclick attribute from the submit button.

    best regards

  • #3
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    You need to reverse the logic.

    Do your form processing and authentication first, prior to outputting the html header.

    Depending on the results of the authentication or if this is the initial loading of the script, you'll either issue a redirect, or output the html header and login form with error message as needed.

  • #4
    New to the CF scene
    Join Date
    Feb 2011
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    HI , can you give me any code for Login form.....

    actually i dont know hot redirect the page and all stuff...

  • #5
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Here's an example from one of my scripts.

    Code:
    #!/usr/bin/perl
    
    use warnings;
    use strict;
    use DBI;
    use CGI;
    use CGI::Carp qw(fatalsToBrowser warningsToBrowser);
    use CGI::Session;
    use HTML::Template;
    use Crypt::PasswdMD5;
    
    my $title     = 'Email Administration Login';
    my $cgi       = CGI->new;
    my $self      = $cgi->url;
    my %login     = $cgi->Vars;
    my $session   = CGI::Session->new or die CGI::Session->errstr;
    my $template  = HTML::Template->new(
                          filename          => '../../html/emadmin/login.tmpl',
                          associate         => [$session],
                          die_on_bad_params => 0,
                          global_vars       => 1,
                          cache             => 0,
                    );
    
    $session->clear if $cgi->param('logout');
    $session->param('hostname', `hostname`);
    
    $SIG{__DIE__} = \&dying;
    
    if ( $cgi->param('Login') ) {
       my $home = 'http://emailadmin.mycompany.com/admin/search.pl';
       print $cgi->redirect($home) if user_authenticated();
    }
    
    print $session->header;
    warningsToBrowser(1);
    print $template->output;
    
    exit;
    
    ################################################################################
    
    
    sub user_authenticated {
    
       $session->param('login_failed',
                       'Invalid username, or password...Please try again');
    
       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->clear('login_failed');
                $session->param('logged_in', 1);
                $template->param('logged_in', 1);
                $session->param('admin', $login{'username'});
                $session->param('roll', $roll);
                $session->param('gic', 1) if $roll eq 'admin';
                return 1;
             }
          }
       }
       return 0;
    }

  • #6
    New to the CF scene
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hello FishMonger,
    Can you please post the .tmpl file too ? :-)
    Thanks a lot in advance :-)

    Quote Originally Posted by FishMonger View Post
    Here's an example from one of my scripts.

    Code:
    #!/usr/bin/perl
    
    
    my $template  = HTML::Template->new(
                          filename          => '../../html/emadmin/login.tmpl',
                          associate         => [$session],
                          die_on_bad_params => 0,
                          global_vars       => 1,
                          cache             => 0,
                    );

  • #7
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    I'm not in the office today, but will post an example tomorrow.

  • #8
    Super Moderator
    Join Date
    May 2005
    Location
    Southern tip of Silicon Valley
    Posts
    2,877
    Thanks
    2
    Thanked 164 Times in 159 Posts
    Code:
    <tmpl_include name="doctype.tmpl">
    <body>
    
    <tmpl_include name="banner.tmpl">
    
      <!-- begin main content container -->
      <div id="content">
        <h2>Login</h2>
        <form method="post" action="http://your-company.com" enctype="multipart/form-data" name="login">
          <fieldset>
          <p>Username: <input type="text" name="username" /></p>
          <p>Password: <input type="password" name="password" /></p>
          <p><input type="submit" name="Login" value="Login" /></p>
          </fieldset>
        </form>
        <tmpl_if name="login_failed"><h4><tmpl_var name="login_failed"></h4></tmpl_if>
      </div>
      <!-- end main content container -->
    
    <tmpl_include name="footer.tmpl">


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •