×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

User Auth in Perl

How can I do a user login? by Siberdude
Posted: 24 Jun 01

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.

#!/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

Back to Perl FAQ Index
Back to Perl Forum

My Archive

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close