Thanks for the post...
I started this script previously, using "fopen" instead of "file" (which I didn't know about). An html file sends the name and password as POST. I changed a few thigs around right now and it seems to work ok, except for one thing - if the password is "saturday" and I enter it in correctly there's no problem. If I enter the wrong password I get "Authorization Required"... again, no problem. However, if I enter "saturday1111", or anything else for that matter after the word "saturday", I still get "You are authorized!"... even though the password isn't correct. Any idea what's up with that?
Thanks again, this was driving me crazy!!!
<?php
$name = $_POST['name'];
$password = $_POST['password'];
// Read the entire file into the variable $file_contents
$filename = '/path/to/.htpasswd';
$fp = fopen($filename, 'r');
$file_contents = fread( $fp, filesize($filename));
fclose($fp);
// Place the individual lines from the file contents into an array.
$lines = explode ( "\n", $file_contents );
// Split each of the lines into a username and a password pair
// and attempt to match them to $PHP_AUTH_USER and $PHP_AUTH_PW.
foreach ( $lines as $line ) {
list( $htname, $htpassword ) = explode( ':', $line );
if ( $htname == "$name" ) {
// Get the salt from $password. It is always the first
// two characters of a DES-encrypted string.
$salt = substr( $htpassword , 0 , 2 );
// Encrypt $PHP_AUTH_PW based on $salt
$enc_pw = crypt( $password, $salt );
if ( $htpassword == "$enc_pw" ) {
// A match is found, meaning the user is authenticated.
// Stop the search.
$auth = true;
break;
}
}
}
if ( ! $auth ) {
echo 'Authorization Required.';
exit;
} else {
echo '<P>You are authorized!</P>';
echo $htpassword;
}
?>