Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Login Please!!

by eoin (Monk)
on Dec 30, 2002 at 22:37 UTC ( #223202=perlquestion: print w/replies, xml ) Need Help??
eoin has asked for the wisdom of the Perl Monks concerning the following question:

I have this basic user authentication program here but it won't work. I continuesly get an invalid user\password message. The password file, 'pass', is written like this (example) eoin pass test 1234 admin sys Here's the code for the program. a fellow monk help me structure it because this is my first proper cgi prog.
use strict; use warnings; use CGI; my $q = new CGI; my $user = $q->param('user'); my $password = $q->param('pass'); #----------------------------------------- #START OF MAIN PROGRAM #----------------------------------------- my ($title,$body); if ( &verify($user,$password) eq "Valid" ){ ($title,$body) = &valid(); } else { ($title,$body) = &fail(); }; print qq(Content-type: text/html\n <html> <head><title>$title</title></head> $body </html>); #----------------------------------------- #END OF MAIN PROGRAM:BEGIN OF VERIFICATION #----------------------------------------- sub verify{ my ($user,$password) = @_; open(PASS, "pass") or die "Couldn't find password file.\n"; while(<PASS>){ chomp; my ($ur,$ps) = split(/\t/, $_, 2); if ( ($ur eq $user) && ($ps eq $password) ){ close PASS; return "Valid"; } } close PASS; return "notValid"; } #----------------------------------------- #IF VALID #----------------------------------------- sub valid { my $title = "Login successful !"; my $body = q(<body bgcolor="black" text="red"> <h3>Login succesful!</h3><br><br> <a href="www.eircom.net"><h2>Click here to continue</h2></a><hr> </body>); return ($title,$body); } #----------------------------------------- #IF NOT VALID #----------------------------------------- sub fail { my $title = "Login unsuccessful !"; my $body = q(<body bgcolor="black" text="red"> Invalid username\password.<br> Please try again.<br><br><hr> <form method=POST action="http://eoinmurphy.netfirms.com/cgi-bin/main. +pl"> User Name: <input name="user" size="30"><br> Password: <input name="pass" size="30"><br> <input type="submit" value="Send"></form><hr> </body>); return ($title,$body); } #----------------------------------------- #END #-----------------------------------------
I mustn't be reading the password file properly, I don't know.... Here's the html code for the form.
<form action="http://eoinmurphy.netfirms.com/cgi-bin/main.cgi" method= +"POST"> <p>User Name: <input type="text" size="30" name="user"><br> Password:<h7>.</h7> <input type="text" size="30" name="pass"><br> <input type="submit" value="Send"> </p> <hr> </form>

Replies are listed 'Best First'.
Re: Login Please!!
by Trimbach (Curate) on Dec 30, 2002 at 22:50 UTC
    Your code is fine... I just ran it on my machine and it worked perfectly without my changing anything. I suspect your problem is in your pass file... mine looked like this:
    gary\ttest cheese\trock
    ...and that's it. (The \t's are actual tabs... I've just put them in like this so you can see them.) Are you running the CGI on a Windows webserver or a Unix server? If you're creating your password file in Windows and uploading it to a Unix server without changing the line endings you'll have problems.

    Hope this helps a little. Good luck!

    Gary Blackburn
    Trained Killer

      I am Trimbach, yea. How do I change the line endings to suite uploading from Windows to Unix to make it work. Thanks. Eoin

        You don't need to change any files, just make sure that text files are transfered in ASCII mode.

        Hope this helps, -gjb-

Re: Login Please!!
by dws (Chancellor) on Dec 30, 2002 at 23:54 UTC
    If the lines in your password file really look like   eoin pass test 1234 admin sys
    then   my ($ur,$ps) = split(/\t/, $_, 2);
    isn't what you want to do. Assuming that you really do have tabs in the file (and not spaces), the third argument causes you to be left with
    $ur = "eoin"; $ps = "pass\ttest\t1234\tadmin\tsys";
    (For an explanation of why this is, consult the description of "split" in perlfunc.)

    Drop the third argument, and you'll be O.K.

    Stylistic comment: Consider returning a boolean (i.e., 0 or 1) instead of "Valid"/"notValid". There's far less chance of subtle typos causing you hours of debugging grief.

Re: Login Please!!
by poj (Parson) on Dec 30, 2002 at 23:11 UTC
    Hello eoin, I recognised that code .. as my suggestion !
    The problem is here my ($ur,$ps) = split(/\t/, $_, 2); which expects your password file to be tab delimited like this username<tab>password
    I don't think yours is. If it is space delimited then use my ($ur,$ps) = split(' ',$_,2);
    If you have more than 2 fields then add $null1, $null2 as required.

    poj
      If you have more than 2 fields then add $null1, $null2 as required.

      Picking nits, I know, but how about adding undef, undef as required? I hate to see values assigned to variables that won't be used.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://223202]
Approved by Mr. Muskrat
help
Chatterbox?
[Corion]: I guess I should not read some discussions on HackerNews - the crowd there is weirdly different - "you have to stay current in tech" (by following all the latest JS trends) is a meme there, but it doesn't really match my experience
[Corion]: At least in my area, experience can beat most new tech anyway because most new tech is just a rehash of things abandoned in the 70s and we still have a lot of that ;)
[Corion]: choroba: Ah, that will be next Friday for $work ;) Drinking enough water is key ;))
[Corion]: But then, maybe that's just my experience with things, and maybe Hacker News is just people much younger who haven't seen more than one tech cycle...
[choroba]: s/water/bear/ and s/enough/too much/
[choroba]: beer
[choroba]: oh ok
[marto]: I crashed the ScotLUG Christmas night, having never actually been to ScotLUG
[choroba]: Corion yeah, I probably already told you about how Bjarne Stroustrup was asked whether he still watched the new tech trends and what really impressed him
[choroba]: His reply was "I watch them, but I haven't been impressed in the last 10 years. There's been nothing new". That was 2 years ago :)

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-12-09 10:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:













    Results (150 votes). Check out past polls.