Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

file based authentication

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

file based authentication

Postby nick_k » Sat Mar 15, 2003 10:16 am

hi, i found this code in a book, heres part of it:

Code: Select all
if(!$fh = fopen($password_file, "r")) { die("<P>Could Not Open Password File"); }
   $match = 0;
   $password = md5($password);
   while(!feof($fh)) {
      $line = fgets($fh, 4096);
      $user_pass = explode(":", $line);
      if($user_pass[0] == $login) {
         if(rtrim($user_pass[1]) == $password) {
            $match = 1;
            break;
         }
      }
   }
   if($match) {
      return 1;
   } else {
      return 0;
   }

if i understand correctly, it opens the file, and gets the first line, username and password, but if theres nore than one line how can it get the correct combination?
say for example you have this in the passwd file:
nick:123456
jack:password

and the user enters the username as jack, how would it know that it has to get the second line?
nick_k
New php-forum User
New php-forum User
 
Posts: 34
Joined: Wed Dec 25, 2002 6:04 am
Location: The Netherlands

Postby Skeletor » Mon Mar 17, 2003 4:28 am

********************
this is prolly more then you asked for
********************
It starts off by opening the filename stored in the $password_file variable.

It sets $match to zero and md5() the password. If you are using this code,
it would probably be best to use something like $_REQUEST['password'] to
get your password and $_REQUEST['username'] to get your username. The above
code you provided will probably give you an error if you don't have global
variables = on.

It will loop through the file with the while(!feof($fh)) loop. this will
continue to loop until it reaches the end of the file. It reads in one
line at a time and then explodes the line into the array $user_pass based
on the ':' delimiter. It will test to see if the $username is equal to
the first element (element 0) in this newly created $user_pass array, and
if it is, it will test to see if the password is equal to the trimmed value
of the 2nd element (element 1). If both username and password match, it
sets $match to 1 and breaks out of the loop. If they are not, it will read
in the next line of the file, explode it into the array, and start the check
all over again.

After the loop, it just checks match. If match is 1, it returns on, if it
is not, it returns zero. This code can be sumed by saying:
return $match
Since match is either 0 or 1, and it returns either a 0 or 1, you mise well
return $match and save yourself a test.

And it's probably good to put a fclose() after your while loop to close
your file. And good practice not to assume global variables are set to on,
so I'd recommend using $_REQUEST, $_POST, or $_GET to refer to your variables
you pass from other pages.
Skeletor
New php-forum User
New php-forum User
 
Posts: 147
Joined: Tue Nov 19, 2002 7:47 am
Location: US, Ohio

Postby nick_k » Mon Mar 17, 2003 6:14 am

Thanks Skeletor! I apreciate you replying
nick_k
New php-forum User
New php-forum User
 
Posts: 34
Joined: Wed Dec 25, 2002 6:04 am
Location: The Netherlands


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 2 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.

cron