Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I do a user login?

User Auth in Perl

How can I do a user login?

by  Siberdude  Posted    (Edited  )
after answering this several times. i thought it would be easier to write a quick FAQ for it.
it asumes the login form names the login field userid and the password field pass, and that your using flatfile database, with the users user name as the filename.
Code:
#!/usr/local/bin/perl
#this line tells the server that your using perl.

print "content-type: text/html\n\n";
# this sends out a greeting, and tells the browser your going to give it html content
my $in;
if ($ENV{'REQUEST_METHOD'} eq "GET") {
$in = $ENV{'QUERY_STRING'};
} else {
$in = <STDIN>;
}
# looks complicated, don't worry about how or why it works, basically it takes the data passed to the form that called the script, and dumps it into a varible ($in)


$in =~ s/\+/ /gi;
$in =~ s/%(..)/pack("c",hex($1))/ge;
#this takes the data and brings it into a more handleable english format (escapes it)


$data_in = split (/&/, $in);

for($i = 0; $i <= $#data_in; $i++)
{
@pairs = split (/=/, $data_in[$i]);
$data{$pairs[0]} = $pairs[1];
}

# this takes all the inputed query string and puts it into an aso array called data, using the form field name as its key

open(AUTH, "$data{userid}.dat");

@userinf = <AUTH>;

close (AUTH);

#we open the users dat file, if it exists we dump the contents into an array, by line.

if($userinf[0])
{
# if the varible $userinf[0] exists (it won't if the file wasn't opened) then we do the stuff inside

if($data{pass} eq $userinf[1])
{

# this checks if the password they sent is the same as the one we have stored for them, if it does we print out a welcome message.

print "congrats you logged in!";

}

else

{

# if they got the password wrong, it isn't the same as the one we have stored for them, we print out something telling them so

print "inncorrect password!";

}

}

else

{

#this is the else for the first if statement (getting confusing now) tells them that they got the username wrong

print "Username not found!";

}

You can, of course modify the output field and make it read what ever you need (a duplicate login box with the password field highlighted for example)

Hopefully this gives you what you need.

Sib
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top