Help! Login script...

They have: 117 posts

Joined: Mar 2000

Here's my script I'm trying to get to work so users can login to my site:

print "Content-type: text/html\n\n";
open(MEMBERS, "$data_path/members.txt");
if ($flock eq "y") {
flock MEMBERS, 2;
}
@members = <MEMBERS>;
close(MEMBERS);
foreach $line (@members) {
($membernum, $membername, $blabla) = split(/\|/, $line);

if ($membername eq $FORM{'username'}) {
open (DATA, "$data_path/$membernum/info.txt");
if ($flock eq "y") {
flock DATA, 2;
}
@data = <DATA>;
close(DATA);

$confirmed = @data[0];
$username = @data[1];
$email = @data[2];
$password = @data[3];
$sitename = @data[4];
$siteurl = @data[5];

if ($FORM{'action'} eq "") { &login; }
else {
if ($FORM{'password'} eq "$password") {
  &header;
&menu;
&middle;
  &enter;
&footer;
exit;
}
        else {
  &badpassword;
  exit;
  }
}
}
else {
$nomember = 1;
}
}

if ($nomember = 1) {
&header;
&menu;
&middle;
print "<FONT FACE=\"Verdana\" SIZE=\"2\">Sorry, there is no user with that username.\n";
&footer;
}

sub badpassword {
&header;
&menu;
&middle;
print "<B><FONT FACE=\"Verdana\" SIZE=\"2\">Wrong Password</B><BR>Sorry,\n";
print "but the password you entered was incorrect.<BR><BR>$FORM{'password'}<BR>$password</FONT>\n";
&footer;
}

sub login {
&header;
&menu;
&middle;
print "<B><FONT FACE=\"Verdana\" SIZE=\"2\">Error</B><BR>Sorry,\n";
print "but you must login before you can have access to this section of our site.</FONT>\n";
&footer;
exit;
}

sub enter {
print "<B><FONT FACE=\"Verdana\" SIZE=\"2\">Welcome, $username</B><BR>\n";
print "You have successfully logged in!</FONT>\n";
}
'

I've been fiddling around with it for a while now, but no luck. So, any help with figuring out what's wrong would be GREAT! Thanks!!

--Edge

Ken Elliott's picture

They have: 358 posts

Joined: Jun 1999

Edge,
That is alot of code for just user : password protection. Here is a simple script that I use for user : password protection, and it is all kept in a text file. Just edit it as you see fit. Also I am typing this script as I go, so there might be a few minor errors. Also I don't see your Form Parser subroutine..so I named mine &Parse

#!/usr/bin/perl -w

&Parse

$username = $FORM{'username'};
$password = $FORM{'password'};

if ($username eq "") {
&error('Sorry you must provide a username');
} elsif ($password eq "") {
&error('Sorry you must provide a password');
}

#User name and passwords should be written to the
#text file in this method " username|password "
#with a "|" bar seperating them

open (DATAFILE, "$data_path/members.txt");
flock (DATAFILE, 2);
@userpass = ;
flock (DATAFILE, 8);
close (DATAFILE);

foreach $line(@userpass) {
@pair = split (/\|/, $line);
if ($pair[0] =~ m/$username/) {
$user = "true";
if ($pair[1] =~ m/$password) {
$pass = "true";
} else {
$pass = "false";
}
}
}

if ($pass eq "true" && $user eq "true") {
print "Content-type:text/html\n\n";
print "Location: protectedpage.html\n\n";
} elsif ($pass eq "false") {
&error('Sorry but your password does not match any password in our database. Please check your password again.');
} elsif ($user ne "true") {
&error('Sorry but your username does not match any username in our database');
}

sub error {
print "Content-type:text/html\n\n";
print "SORRY THERE WAS AN ERROR:$_[0]";
}

##### END OF SCRIPT #####
Hope you can use that.

Pimpin like a pimp with an electrofied pimpin machine!

They have: 850 posts

Joined: Jul 1999

Edge, here are a few suggestions:

when opening a file, always use 'or die()'. it always helps ins troubleshooting errors.
open (DATA, "$data_path/$membernum/info.txt") or die "Cannot open $data_path/$membernum/info.txt to read: $!";

Second, regarding $confirmed = @data[0]; and the other variables defined in the same way, try replacing @data with $data.

Lastly, with if ($nomember = 1), you should use == not =.

Just a quick note, it is always good to indent your code to keep it more organized and easier to read.

They have: 117 posts

Joined: Mar 2000

Okay, I got it working... almost. I think the only reason it's not working now is that $password contains a line break at the end. How do I remove it?

--Edge

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.