I have made a script that reads a .htpasswd file and validate username and password.
It works fine with my own local .htpasswd file but then I use a .htpasswd file downloaded from my host it does not work.
The files are identical exept that the file from the server is 1 byte bigger per line, meaning that if there's 10 lines in passwordfile the filesize is 10 bytes bigger.
If I open the files in a texteditor they are 100% identical.
How can it be? I think thats why my script does not work on my server.
My script looks like this:
$auth = false;
$filename = '.htpassfile';
$file = fopen($filename, "r");
$file_contents = fread( $file, filesize( $filename ) );
fclose( $file );
// 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
foreach ($lines as $line) {
list($htuser, $htpass) = explode(':', $line);
if ($htuser == $username) {
if ($htpass == crypt($password,$htpass)) {
// A match is found, meaning the user is authenticated.
// Stop the search.
$auth = true;
break;
}
}
}