PERL log-in

He has: 1,380 posts

Joined: Feb 2002

hi (again)...lol
i was wondering if you guys could help me get started with a login PERL script that crosschecks with a flatfile (.txt) database, and that can be submitted to.

example:
i have one page with a log-in...the user name and password has to be signed up for (the submitted to). the user name and password have to be in the database, and these are required to view pages.

how would i do this? any ideas? any help sites i can check?
thanks for all the help!

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

Create a file called data.txt and separate username and password by pipes (|)
Have PERL open the data.txt file, put the contents into an array, split the contents by pipes int an array called @this
Set the form data to $username and $password
Write an IF loop:

if ($username == $this[0] && $password == $this[1]) {
print "the login worked";
} else {
print "login didn't work";
}
'
I think thats it... I haven't worked with PERL in a while

Laughing out loud

He has: 1,380 posts

Joined: Feb 2002

how would i execute it...and if it works, how would i re-direct them to another page?

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

OK... here's the entire code
[edit]
I didn't do it right... here's the right code
OK I totally messed this up... how do you pass form data?? Thats how it worked back in August 2000 when I used this script....

#!/usr/local/bin/perl
#Set password and loginname to the name="" part of your HTML form
$pass = $formdata{'password'};
$name = $formdata{'loginname'};
#That uses formdata, which is older, but still effective
open (DATABASE, "< ../database.txt") || &Error;
flock (DATABASE, 2);
@contents = <DATABASE>;
flock (DATABASE, 8);
close (DATABASE);
foreach $item(@contents) {
@items = split(/\|/, $item);
if ($name eq $items[0]) {
$thepass = $items[1];
chomp($thepass);
if ($pass eq $thepass) {
$pw = "true";
}
$un = "true";
}
}

if ($un eq "true" && $pw eq "true") {
HERE'S WHERE THE CODE FOR SENDING THEM TO THE PAGE GOES... SOMEONE ELSE HELP ME OUT HERE
} else {
&mime;
print "Your username and password didn't work";
}
sub mime {
print "Content-type:text/html\n\n";
}
sub Error {
&mime;
print "<center>";
print "THERE WAS AN <h2>ERROR</h2> PROCESSING THE DATABASE";
exit;
}
'

This is how it worked for me a while ago... let me know how it works

Laughing out loud

Mark Hensler's picture

He has: 4,048 posts

Joined: Aug 2000

I believe the redirect code for PERL is simply printing a HTTP header...

print "Location: next_page.html\n\n";

The two newlines are important.

They have: 601 posts

Joined: Nov 2001

Hi Nike

Good job with your code! Unfortunately, there were a few grey areas I think you bumped into. I've done some fixes and additions and here's the code I've come up with. This should work. Cheers!

#!/usr/local/bin/perl

use strict;

use CGI;
my $query = CGI->new();

# now we retrieve the information passed to the script and
# place it into our own variables.

my $pass = $query->param('password');
my $name = $query->param('loginname');

# where's our database stored?
my $file = "/path/to/database";

# now we open up the database and slurp contents into
# @contents. We use $/ to enable slurp mode.

my @contents = ():

{ local $/;

open (DB,"<$file") or die $!;
@contents = <DB>;
close DB;
}

# now we've got the data out of the file, let's loop through
# it and see what happens.

foreach my $line (@contents) {

chomp $line;
my ($db_name,$db_pass) = split(/\|/, $line);

# define url yo send the user to if authentication is OK.
my $url = "http://you.have.been.authenticated.com/";

if (($db_name eq "$name") and ($db_pass eq "$pass")) {
print $query->redirect($url);
}

else {
print $query->header();
print "Your username and password didn't work";
}

}
'

- wil

He has: 1,380 posts

Joined: Feb 2002

ok...i think i can handle modifying the code...now how do i run it?

He has: 1,380 posts

Joined: Feb 2002

one more question...how do i mark pages that need a user name and password?

They have: 601 posts

Joined: Nov 2001

How do I run it?

You save the above code I posted into a file called login.cgi. You save this file in your webscape, uploading using ASCII transfer mode and CHMODing to 755.

You create a webpage with two input fields on it. One called loginname and the other called password. You direct the form to the script using and then watch the script do it's stuff.

Remember you need to change a few variables in the script - namely $file and $url.

how do i mark pages that need a user name and password?

Sorry, now you've lost me. Can you explain a little more clear what you mean by this? Thanks.

- wil

He has: 1,380 posts

Joined: Feb 2002

if people want to look at a page, but i want only the user's (w/ member name and password) to look at it...how can i select which ones need a username and such?

nike_guy_man's picture

They have: 840 posts

Joined: Sep 2000

You could do this very easily with PHP
You could even set the cookie with the PERL, then recall it with PHP....
Every page you want restricted access to would have to be renamed to a .pl or .cgi, or .php if you use PHP.
You could also use a .htpasswd I think... although that's kind of long and hard to do

Laughing out loud

They have: 601 posts

Joined: Nov 2001

This is what you asked in your original question, for which I provided code for above?

He has: 1,380 posts

Joined: Feb 2002

yes..i got the code, i was just wondering how to setup which pages need a password and which dont

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

Usually (my favourite caveat) you would password protect a directory, not a file. So you would set up a directory and put all the files in that directory that you want to keep safe from prying eye.

Does that help?

They have: 601 posts

Joined: Nov 2001

I'll have to second Suzanne here Smiling

He has: 1,380 posts

Joined: Feb 2002

what if this page was for the users to access it...not just me...but i still want to use PERL...isnt there some simple tag like <password>' or something, that marks a page, telling the server it needs a password to be accessed, and it redirects them to a login page...like the skateboard.com website or almost any other website i can think of

Suzanne's picture

She has: 5,507 posts

Joined: Feb 2000

that's more complex than using .htaccess -- it would require hidden fields or cookies to track the referring page so you can push people back there once they've logged in.

You would have the test of logging in-ed-ness in the header. However, I've never done that, because I'm not a programmer. I've seen it done with Perl, Java and PHP, but I don't know exactly how it works.

There is no easy tag for it, though.

This sort of system is the essence of CGI.

He has: 1,380 posts

Joined: Feb 2002

so i can do it....does anyone know how? please help me!

lol

He has: 1,380 posts

Joined: Feb 2002

one more thing...how would i automatically add to this database (preferably using PERL)...through a form?

thanks for everyones help...thanks ALOT!Laughing out loud

They have: 601 posts

Joined: Nov 2001

OK. You're asking a lot here! Smiling

1. The way I would do this would be to pass every page request through my perl script. This script would then parse every page and look for the tag , and if found ask for a login/password. If not, then let the user view the page as normal.

I have actually done something very similar before, but I wouldn't do it again; mainly because there are much better systems. If you stick to htaccess authentication you'll save yourself a) reinventing the wheel, b) a lot of bother and c) you will save a lot of server resources. Parsing every page through a perl script just to look for a tag can take up a lot of your memory - especially if the pages are long.

2. I don't know what you mean by 'automatically add to this database', sorry? What exactly is 'this' you want to add to a database?

- wil

He has: 1,380 posts

Joined: Feb 2002

'this' is information the user submits through a sign-up form, sending data into a database...which i would like it to submit into THE database that we have been working on here...

also...i dont really like the .htaccess, i think it looks unprofessional

They have: 601 posts

Joined: Nov 2001

Ok.

So, you have a database set up? What kind of database is it? What is the strucutre of the database? Where is the form that people send the information from?

- wil

He has: 1,380 posts

Joined: Feb 2002

will be flat file...i havent yet because i dont know how to separate the data...i have made a form (but no form path), cuz i dont know what to use

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.