Home: Perl Programming Help: Beginner:
multiple user/pass's in a file...



ultimategamer2k
User

Apr 20, 2001, 1:41 PM


Views: 361
multiple user/pass's in a file...

okay, i have a file set up like this
user:pass:user:pass:user:pass:user.... etc
what would be the code if i wanted to have something like this

if (($user eq $users) && ($pass eq $passes)){
do something;}
else { do something else;}

so i want to be able to take each and every user from that file, and match it with what evers inputted through the form, and same with the pass...
... this probably doesnt make much sense, so ask if you need clarification

-------------------------------------------------------------
visit my web site at <a href=http://ultimategamer.cjb.net>here</a>


wickedxter
User

Apr 20, 2001, 8:37 PM


Views: 360
Re: multiple user/pass's in a file...

sub check
{
$username = $_[0];
$password = $_[1];

open(FILE,"/path/to/file");
@data = <FILE>;
close(FILE);
foreach $line (@data){
($user, $pass) = split(/\|/,$line);
if(($username eq "$user") && ($password eq "$pass")){
$flag = 1;
}
else {
$flag =0;
}
}
}

call it like
&check("$FORM{'username'}","$FORM{'password'}");
if($flag eq "1"){
#do something
}
else {
print "Error: Wrong Password and Username";
}



zanardi
journeyman

Apr 21, 2001, 9:46 PM


Views: 351
Re: multiple user/pass's in a file...

uh bad idea

if your file looks like this:


Code
user name:password:user name:password:user name:password:user name:password:user name:password

your going to run into serious problems, 1 you either put a different user on each line, or 2 you put something inbetween each user account so you can split up the users, then check there name/password

ok, now I suggest you do something like this:


Code
user name:password 
user name:password
user name:password

anyway, this is my version of wicked's code:


Code
check($FORM{'username'},$FORM{'password'}); 

if ($flag) {
print 'yay';
} else {
print 'Error: Wrong Password and Username';
}

sub check {
my ($username,$password) = @_;

open(F,'/path/') or die $!;
@data = <F>;
close F;

$flag = 0;
foreach (@data){
chomp $_;
($user,$pass) = split(/:/,$_);

if (($username eq $user) && ($password eq $pass)) {
$flag = 1;
}
}
}

----------------------
Fueled By ZCom