Click to See Complete Forum and Search --> : Single to multiple users


edatz
02-14-2009, 09:22 AM
Hi, I've got an old script that I use fine (nice and simple) where I can log in and do stuff. No problem at all.

However, it only allows me to use it and I would like to make it so more than one person can log in.

The login for the script is currently like this


# username and password
# How do I check username and password against a file instead of this?
$username = "aaa";
$password = "111";


When I log in with aaa and 111, it takes me to a sub:

sub checkUser {
if ($input{'username'} ne $username || $input{'password'} ne $password) {
print "ACCESS DENIED";
exit(0); #### Note-this exits to the rest of the script and it can be used
}
}


Every sub routine in the script has checkUser first - no login no use.

How can I make this so I can have more than one user?

I'm assuming a d simple file (users.txt) like this for the users;
aaa|111
bbb|111
ccc|111

I did try this but it had the total opposite effect:

sub checkUser {
$userfile = "users.txt";
open (UDB,"$userfile") || die("Cannot open $userfile");
@USF = <UDB>;
close (UDB);
foreach $usr (@USF) {
chomp;
($username,$password)=split(/\|/,$rec);
if ($input{'username'} ne $username || $input{'password'} ne $password) {
print "ACCESS DENIED";
exit(0); #### Note-this exits to the rest of the script and it can be used
}
}
}


Can someone help please?
Thank you

scragar
02-14-2009, 09:38 AM
@logins = (
[ "User1", "password1" ],
[ "User2", "password2" ],
[ "User3", "password3" ]
);

sub checkUser {
for $i ( 0 .. $#AoA ) {
if( $input{'username'} eq $logins[$i][0]
&& $input{'password'} eq $logins[$i][1] )
return $i;
}
print "ACCESS DENIED";
exit(0);
}

And with a text file something like:
sub checkUser {
$userfile = "users.txt";
open (UDB,"$userfile") || die("Cannot open $userfile");
@USF = <UDB>;
close (UDB);
foreach $usr (@USF) {
chomp;
($username,$password)=split(/\|/,$rec);
if($input{'username'} eq $username && $input{'password'} eq $password)
return 0;
}
print "ACCESS DENIED";
exit(0);
}

edatz
02-14-2009, 12:04 PM
Neither one of those work. It doesn't like return.

It says syntax error near "} return

scragar
02-14-2009, 12:33 PM
Yeah, my mistake, try this(tested this time :p):
#!/usr/bin/perl
use strict;
my @logins = (
[ "User1", "password1" ],
[ "User2", "password2" ],
[ "User3", "password3" ]
);

my %input = (
username=>"User1",
password=>"password1"
);
&checkUser;
print "logged in as user 1";
%input{username} = "User2";
&checkUser;
print "logged in as user 2";

BEGIN{

sub checkUser {
my $i;

for $i ( 0 .. $#logins ) {
if($input{'username'} eq $logins[$i][0]
&& $input{'password'} eq $logins[$i][1] ){
return $i;
}
}
print "ACCESS DENIED";
exit(0);
}
}
Or for from file:
#!/usr/bin/perl
use strict;
my %input = (
username=>"User1",
password=>"password1"
);
&checkUser;
print "logged in as user 1";
%input{username} = "User2";
&checkUser;
print "logged in as user 2";

BEGIN{
sub checkUser {
my $userfile = "users.txt";
my ($usr, $username, $password);
open (UDB,"$userfile") || die("Cannot open $userfile");
@USF = <UDB>;
close (UDB);
foreach $usr (@USF) {
chomp;
($username,$password)=split(/\|/,$rec);
if($input{'username'} eq $username
&& $input{'password'} eq $password){
return 0;
}
}
print "ACCESS DENIED";
exit(0);
}
}

edatz
02-14-2009, 01:10 PM
I tried it and got about 40 lines of reasons why I could not use it. Figured it was because of the strict. So tried just the code you posted in it's own script and it doesn't like the percent sign.

This so far above my head that we'll call it a day and I'll just stick the whole thing inside a protected folder and give everyone the standard username and password.

Thanks for your time and effort. Not your fault - I'm an artist not a programmer.

scragar
02-14-2009, 01:13 PM
Oh, poopy, my mistake, again. It's
$input{username} = "User2";
Not %, I forgot to change that when I was editing it down, it should work if you make that edit.