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!
Authenticating UNIX password
Moderator: General Moderators
I think that should work.
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.
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;
}
}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!
Wow, thanks a lot!!!!!!!