Stick this in the top of your php page and you get a browser authentication dialog. It should work with regular .htpasswd files and .group files.
The passwords generated here work okay.
PHP Code:
$AuthUserFile = file("/path/to/.htpasswd");
$AuthGroupFile = file("/path/to/.groups");
$group = "groupname";
$realm = "Authorisation Required";
function authenticate(){
header("WWW-Authenticate: Basic realm=\"$realm\"");
header('HTTP/1.0 401 Unauthorized');
echo "You must enter a valid user name and password to access the requested resource.";
exit;
}
for(; 1; authenticate()){
if (!isset($HTTP_SERVER_VARS['PHP_AUTH_USER']))
continue;
$user = $HTTP_SERVER_VARS['PHP_AUTH_USER'];
if(!preg_grep("/\b$user\b/", preg_grep("/^$group:/", $AuthGroupFile)))
continue;
if(!($authUserLine = array_shift(preg_grep("/$user:.*$/", $AuthUserFile))))
continue;
preg_match("/$user:((............).*)$/", $authUserLine, $matches);
$authPW = $matches[1];
$salt = $matches[2];
$submittedPW = crypt($HTTP_SERVER_VARS['PHP_AUTH_PW'], $salt);
if($submittedPW != $authPW)
continue;
break;
}
echo "Password Verified!"
?>
I took some of the code from php.net user comments. But the regex part was wrong in the examples given, so it was a pain to debug. It's helped me learn a bit about regex, preg_grep, crypt and salts.
I hope my pain is of some use to someone else
Bookmarks