Authenticating UNIX password

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tahmed
Forum Newbie
Posts: 2
Joined: Thu Jun 05, 2003 12:29 pm
Location: Tracy, CA
Contact:

Authenticating UNIX password

Post by tahmed »

Howdy. Does anyone have a code snippet, or function that can take a username and password and check if it validates on a unix box? I tried searching around, and just find lots of stuff that checks against a database.

I'm new to PHP, and have been programming in it for about 2 days now. Pretty much have my app going and all, just this one particular requirement for the people I'm building it for.

Any clues would be most appreciated! Thanks!
User avatar
delorian
Forum Contributor
Posts: 223
Joined: Sun May 04, 2003 5:20 pm
Location: Olsztyn, Poland

Post by delorian »

I think that should work.

Code: Select all

function check_user_file($sUser_Name,$sUser_Password) {
 
  $sReg_Pattern = "^ї0-9a-z_-]+$";
  if(!eregi($sReg_Pattern,$sUser_Name) && !eregi($sReg_Pattern,$sUser_Password)) { return(0); } 
 
  $fp = @fopen("check_user_file.txt","r") or die; 
  
  while(!@feof($fp)) { 
  
    $sLine = @fgets($fp,100); 
    $aData = explode(":",$sLine); 
    $aDataї0] = trim($aDataї0]); 
   $aDataї1] = trim($aDataї1]); 

    if($sUser_Name == $aDataї0] && md5($sUser_Password) == $aDataї1]) {  
     @fclose($fp); 
     return(1); 
    }  
  }
  
  @fclose($fp); 
  return(0); 
 }



if (!isset($_SERVERї'PHP_AUTH_USER']) { 
    header('WWW-Authenticate: Basic Realm="Private"'); 
    header('HTTP/1.1 401 Unauthorized'); 
    exit; 
} else {
    $status = check_user_file($_SERVERї'PHP_AUTH_USER'], $_SERVERї'PHP_AUTH_PW']); 
    if(!$status) { 
         header('WWW-Authenticate: Basic Realm="Private"');	
         header('HTTP/1.1 401 Unauthorized');
        exit;
    }
}
Passwords are stored inside the text file:

user_name:hashed_user_password


P.S. Sorry there's no comments, but it is my script and all of them were in polish, so probably you wouldn't understand.

BTW: http://www.php.net has a lot of usefull examples and stuff.
tahmed
Forum Newbie
Posts: 2
Joined: Thu Jun 05, 2003 12:29 pm
Location: Tracy, CA
Contact:

Wow!

Post by tahmed »

Wow, thanks a lot!!!!!!!
Post Reply