CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Login script not working - Please Help

 



abockover
Deleted

Sep 24, 2000, 7:58 AM

Post #1 of 3 (1053 views)
Login script not working - Please Help Can't Post

I am working on a simple login script for another program.

The script displays a form with a text and password field asking for a username and password.

Username and password is submitted and the script checks a flat file database for the correct username and password. If a correct username and password is given, their name is displayed.

When I try the script, I can login only as the first user in the database:

bob|1234|Bob Jones

If I try to login as another user:

joe|5678|Joe Shmoe

I get a script generated error (goes back to the login page).

It is only reading the firts line in the database.

Could anyone help?

If you would like to see the script in "action": http://www.crevasoft.com/login.pl

username: bob password: 1234
username: joe password: 5678

Only bob will login.

Below is the script
+++++++++++++++++++++
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


#!/usr/bin/perl

&parse_form;

$userlist = "users.txt";

$username = $input{'lg_username'};
$password = $input{'lg_password'};

print "Content Type: text/html\n\n";

open (USERS,"<$userlist");
@LST=<USERS>;
close (USERS);

foreach $user (@LST){
chomp($user);
($dl_username,$dl_password,$dl_name)=split(/\|/,$user);
if($username eq $dl_username and $password eq $dl_password) {

print "<html><body><h2><i>\n";
print "Hello, you are logged in as $dl_name\n";
print "</i></h2></body></html>\n";

exit(0);
} else {
&loginForm;
exit(0);
}
}

sub loginForm {
print <<"EOF";
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2><hr>
<form action="login.pl" method="POST">
Username: <input type="text" name="lg_username">
<br>
Password: <input type="password" name="lg_password">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
EOF
exit(0);
}

sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$input{$name} = $value;
}
}
</pre><HR></BLOCKQUOTE>
++++++++++++++++++

Below is the flat file datbase the script is suppose to search for usernames and passwords

++++++++++++++++++
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>


bob|1234|Bob Jones
joe|5678|Joe Shmoe
lyn|9101|Lyn Gin
</pre><HR></BLOCKQUOTE>
++++++++++++++++++

Thanks a lot,

Aaron

[This message has been edited by abockover (edited 09-24-2000).]


Kanji
User

Sep 24, 2000, 4:14 PM

Post #2 of 3 (1053 views)
Re: Login script not working - Please Help [In reply to] Can't Post

You have a problem in your logic ...

Inspite of using foreach, if the user isn't correct you exit() the script, which is why only the first user works.

Instead, you want to do something like ...

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

open (USERS,"<$userlist") or die $!;
while (<USERS> ) {
next unless /^\Q$username|$password|\E/;
$logged_in = 1;
last;
}
if ( $logged_in ) {
print "You are logged in";
} else {
print "You need to log in";
}
exit;</pre><HR></BLOCKQUOTE>

... so that you search the enitre list before exiting, not just the first record.


abockover
Deleted

Sep 25, 2000, 11:21 AM

Post #3 of 3 (1053 views)
Re: Login script not working - Please Help [In reply to] Can't Post

What you said to try worked - thank you.

Now, Once the user has been logged in, the script will have to open the users config file (username.cfg).

I am having trouble with this as well.

Could you help me again? Thanks.

Aaron.

-------------------------------

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>



#!/usr/bin/perl

&parse_form;

$userlist = "users.txt";

$username = $input{'lg_username'};
$password = $input{'lg_password'};

print "Content Type: text/html\n\n";

open (USERS,"<$userlist") or die $!;
while (<USERS> ) {
next unless /^\Q$username|$password|\E/;
$logged_in = 1;
last;
}
close USERS;
if ( $logged_in ) {

open (USERLIST,"<$username.cfg") or die $!;
@LST=<USERLIST>;
close (USERLIST);

foreach $reguser (@LST){
chomp($reguser);
($usr_name,$usr_age,$usr_sex)=split(/\|/,$reguser);
print "<html><body><h2><i>\n";
print "Hello, you are logged in as $usr_name. You are a $usr_age year old $usr_sex.\n";
print "</i></h2></body></html>\n";
}

} else {
&loginForm;
}
exit(0);


sub loginForm {
print <<"EOF";
<html>
<head>
<title>Login</title>
</head>
<body>
<h2>Login</h2><hr>
<form action="login.pl" method="POST">
Username: <input type="text" name="lg_username">
<br>
Password: <input type="password" name="lg_password">
<br>
<input type="submit" value="Login">
</form>
</body>
</html>
EOF
exit(0);
}

sub parse_form {

read(STDIN, $buffer, $ENV{'CONTENT_LENGTH'});
if (length($buffer) < 5) {
$buffer = $ENV{QUERY_STRING};
}
@pairs = split(/&/, $buffer);
foreach $pair (@pairs) {
($name, $value) = split(/=/, $pair);

$value =~ tr/+/ /;
$value =~ s/%([a-fA-F0-9][a-fA-F0-9])/pack("C", hex($1))/eg;

$input{$name} = $value;
}
}
</pre><HR></BLOCKQUOTE>

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives