programming-examples/perl/CGI/Verifying a username and a password.pl

79 lines
1.5 KiB
Perl
Raw Normal View History

2019-11-15 12:59:38 +01:00
<HTML>
<HEAD>
<TITLE>Verifying a username and a password.</TITLE>
</HEAD>
<BODY>
Type in your username and password below.
<FORM ACTION = "index.pl" METHOD = "POST">
Username:<INPUT SIZE = "40" NAME = "USERNAME">
Password:<INPUT SIZE = "40" NAME = "PASSWORD" TYPE = PASSWORD>
<INPUT TYPE = "SUBMIT" VALUE = "Enter">
</FORM>
</BODY>
</HTML>
#!perl
use CGI qw(:standard);
$testUsername = param( "USERNAME" );
$testPassword = param( "PASSWORD" );
open ( FILE, "password.txt" ) || die "The database could not be opened";
while ( $line = <FILE> )
{
chomp $line;
( $username, $password ) = split( ",", $line );
if ( $testUsername eq $username )
{
$userVerified = 1;
if ( $testPassword eq $password )
{
$passwordVerified = 1;
last;
}
}
}
close( FILE );
print header;
if ( $userVerified && $passwordVerified )
{
accessGranted();
}
elsif ( $userVerified && !$passwordVerified )
{
wrongPassword();
}
else
{
accessDenied();
}
sub accessGranted
{
print "<TITLE>Thank You</TITLE>";
print "Permission has been granted, $username.";
print "<BR>Enjoy the site.";
}
sub wrongPassword
{
print "<TITLE>Access Denied</TITLE>";
print "You entered an invalid password.<BR>";
print "Access has been denied.";
}
sub accessDenied
{
print "<TITLE>Access Denied</TITLE>";
print "You were denied access to this server.";
}
#File: password.txt
#account1,password1
#account2,password2
#account3,password3