Forum Moderators: coopster & phranque

Message Too Old, No Replies

Perl & Text file based username & password authentication

need a cheap & nasty script to read usernames & passwords from a text file

         

gilli

8:03 am on Apr 3, 2003 (gmt 0)

10+ Year Member



Hi All

Frist time in Perl and PHP CGI Scripting....

I've got a client who needs a very simple login system. They only have access perl on the server & my perl skills are very rusty (they never were great).

Essentiall I'm planning on having a file in the format

username1,password1
username2,password2
username3,password3
etc....

The file will be kept out of the web root & no encryption is needed. The idea is that the client will manually maintain this list using a text editor.

I'll have a simple username & password form that will be posting to the script.

The script itself will read the text file input a hash, loop through and look for matches adn do a conditional redirect to either the target page (on another host) or an error page.

My questions are:

Am I tripping here? Is there an easier way? Does anyone have something lieing around collecting dust on their hard drive?

I'm sure I'll work it out eventually but I've got very little time to put this together & webmasterworld usually comes through for me :)

Thanks in advance

-gilli

gilli

12:50 pm on Apr 3, 2003 (gmt 0)

10+ Year Member



OK this is where I'm at - any comments or points would be appreciated as I'm not really up on best practice.


#!/usr/bin/perl

#################################################
# CONFIGURATION VARIABLES#
#################################################
$passLocation = "http://www.gruden.com";
$failLocation = "http://www.google.com";
$USERFILE = "/home/dpws/data/users.txt";
#################################################

# default values
$isValidUser = 0;

#call form parsing script
require "/home/dpws/cgi-bin/subparseform.lib";
&Parse_Form;

# set up form data
$thisUsername = $formdata{'username'};
$thisPassword = $formdata{'password'};

open USERFILE or die "can't find file: $USERFILE: $!\n";

while (<USERFILE>) {
($username, $password)= split (/:/, $_);
if ($username eq $thisUsername && $password eq $thisPassword) {
$isValidUser = 1;}
close USERFILE;

if ($isValidUser == 1) {
print "Location: $passLocation\n\n";
} else {
print "Location: $failLocation\n\n";
}