http://qs321.pair.com?node_id=1223031

theravadamonk has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,

with CGI, I want to have multiple usernames and passwords from a password.txt file.

In front end I have a login form login.html.

here it is

<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>

here's my login.cgi 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 opene +d"; 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 $passw +ord ) ) { 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 $passw +ord ) ) { 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 );

here's my /tmp/password.txt file

account1,password1

this works for a Single username and passowrd.

When I have multiple usernames and passwords in this way

account1,password1 account2,password2 account3,password3

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

How to solve this? I DO need multiple usernames and passwords