perlquestion
theravadamonk
<p>Hi Monks,</p>
<p> with CGI, I want to have multiple usernames and passwords from a password.txt file.</p>
<p> In front end I have a login form login.html. </p>
<p> here it is </p>
<code>
<HTML>
<HEAD>
<TITLE>Verifying a username and a password.</TITLE>
</HEAD>
<BODY>
Type in your username and password below.
<FORM ACTION = "/cgi-bin/login.cgi" METHOD = "POST">
Username:<INPUT SIZE = "40" NAME = "USERNAME"><br>
Password:<INPUT SIZE = "40" NAME = "PASSWORD" TYPE = PASSWORD><br>
<INPUT TYPE = "SUBMIT" VALUE = "Enter">
</FORM>
</BODY>
</HTML>
</code>
<p> here's my login.cgi code </p>
<code>
#!/usr/bin/perl
use CGI qw(:standard);
use strict;
use warnings;
my $var_username = param( "USERNAME" );
my $var_password = param( "PASSWORD" );
my $url="http://host.redirectdomain.com:9999/";
my $t=1; # time until redirect activates
print "Content-Type: text/html; charset=utf-8\n\n";
open ( FILE, "/tmp/password.txt" ) || die "The file could not be opened";
while ( my $line = <FILE> )
{
chomp $line;
( my $username, my $password ) = split( ",", $line );
if ( ( $var_username eq $username ) && ( $var_password eq $password ) ) {
print "$var_username, $var_password <br>"; # I will remove this in real world
print "Permission has been granted <br>";
print "<META HTTP-EQUIV=refresh CONTENT=\"$t;URL=$url\">\n";
}
elsif ( ( $var_username eq $username ) && ( $var_password ne $password ) ) {
print "$var_username, $var_password <br>"; # I will remove this in real world
print "You entered an invalid password. <br>";
print "Access has been denied. <br>";
}
elsif ( ( $var_username ne $username ) && ( $var_password eq $password ) ) {
print "$var_username, $var_password <br>"; # I will remove this in real world
print "You entered an invalid username. <br>";
print "Access has been denied. <br>";
}
else{
print "$var_username, $var_password <br>"; # I will remove this in real world
print "You were denied access to this server. <br>";
}
}
close( FILE );
</code>
<p> here's my /tmp/password.txt file </p>
<code>
account1,password1
</code>
<p> this works for a Single username and passowrd.</p>
<p>When I have multiple usernames and passwords in this way</p>
<code>
account1,password1
account2,password2
account3,password3
</code>
<p>It check all. The question is if I type username as account1 and password as let's say pass, It prints "You entered an invalid password" and "You were denied access to this server". when I give correct username and password, it redirects as expected but prints the latter else statement. These are issues. If INPUTS are Wrong, How can I have single error message instead of multiple errors. If INPUTS are right, How can I have single message like "Permission has been granted" and redirection to the url </p>
<p> How to solve this? I DO need multiple usernames and passwords </p>