So I've created a directory that I have protected with htaccess. The issue is that I want every user in an SQL table with the 'admin' usertype to be able to use their custom login info. I've set up everything so that the usernames and encrypted passwords should be written to the .htpasswd file (right now I have to run a php script manually to do so, but in the future I'll have the process automated). The issue is that my script doesn't seem to recognize my .htpasswd file as existing. I tried moving the file out of the password protected directory (and changing all references accordingly), but it still won't register the existence of the file.
I'm not sure if this is because I've done something wrong or if php simply can't write to .htpasswd files, or anything in between. Anyway, I'm fairly new to php in the grand scheme of things, and I couldn't find any help just by searching through docs and other forum posts, so I turned to Stack Overflow.
My Code:
//$password is already encrypted
if (!file_exists('/path/to/.htpasswd')) {
die('File does not exist');
} else {
$myfile = fopen('/path/to/.htpasswd') or die('There was a problem opening this file');
$string = file_get_contents('/path/to/.htpasswd') or die('There was a problem getting the contents of this file');
$string = explode("\n", $string);
if (in_array($username.":".$password, $string)) {
die('User already in .htpasswd file.');
} else {
fwrite($myfile, $username.":".$password."\n");
echo('Added user and password to file.');
}
}
Any help is greatly appreciated.