acctman Posted December 12, 2012 Share Posted December 12, 2012 Hi does anyone have an example of using a PHP code to access the .htpasswd file for a user / pass login. Quote Link to comment https://forums.phpfreaks.com/topic/271885-http-authentication-with-php/ Share on other sites More sharing options...
requinix Posted December 12, 2012 Share Posted December 12, 2012 No but if you know what the file is like and how to use it then it's pretty easy to write one. function validate($htpasswd, $username, $password) { // file exists if (!is_file($htpasswd) || !is_readable($htpasswd)) { trigger_error("{$htpasswd} passwd file not found", E_USER_WARNING); return false; } // valid username... docs don't really define this but it can't contain a colon if (strpos($username, ":") !== false) { trigger_error("Invalid username", E_USER_NOTICE); return false; } // search the file foreach (file($htpasswd) as $line) { // lines are in the form "username:hashed password" list($lineuser, $linehash) = explode(":", trim($line)); if ($lineuser == $username) { return crypt($password, $linehash) == $linehash; } } return false; } Quote Link to comment https://forums.phpfreaks.com/topic/271885-http-authentication-with-php/#findComment-1398851 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.