Page 1 of 1

Authenticating UNIX password

Posted: Thu Jun 05, 2003 12:29 pm
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!

Posted: Thu Jun 05, 2003 4:49 pm
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.

Wow!

Posted: Fri Jun 06, 2003 11:20 am
by tahmed
Wow, thanks a lot!!!!!!!