|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2003-07-23 08:31 UTC] iliaa@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Mon Nov 03 04:00:01 2025 UTC |
Description: ------------ This is a functionality enhancement request. There are two ways to authenticate a user with PHP -- flat file and database. If you use a flat file, there is no simple way (ie built in function) to use a flat file already created by the apache program htpasswd for authentication. Instead you have to perform the following steps: 1) Read the file created by htpasswd. 2) Split the file into lines -- each representing a user. 3) Parse each line into a userid and an encrypted password. 4) Read the first two characters of the encrypted password, and use that as the salt to encrypt user provided password. 5) Compare the file's encrypted password to the user provided encrypted password. This is a lot of work for such a common task, and seems like there should be a built in function which takes care of this for example: boolean authenticate_htpasswd(string username, string clear_password, string password_file) Reproduce code: --------------- <? function authenticate_htpasswd ($passwd_file, $auth_passwd = $_SERVER['PHP_AUTH_PW'], $auth_userid = $_SERVER['PHP_AUTH_USER']) { if (file_exists($passwd_file)) { $fp = fopen($passwd_file, "r"); $file_contents = fread($fp, filesize($passwd_file)); fclose($fp); } else { return false; } $line = explode("\n", $file_contents); $i = 0; while($i <= sizeof($line)) { $data_pair = explode(":", $line[$i]); if ($data_pair[0] == $auth_userid) { $enc_file_passwd = $data_pair[1]; $salt = substr($enc_file_passwd,0,2); $enc_auth_passwd = crypt($auth_passwd, $salt); if ($enc_file_passwd == $enc_auth_passwd) { return true; break; } } $i++; } return false; } ?> Expected result: ---------------- Take a string representing a file created by htpasswd and authenticate against either a provided username and password or the _SERVER['PHP_AUTH_*'] variables.