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


In reply to with CGI, How to have multiple usernames and passwords from a txt file (password.txt) file by theravadamonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.