I'm writing a PHP script that updates a .htpasswd file (yes, I know it's not very secure, but I have several protections to prevent people from accessing the script).
Right now, I'm just appending the current file, like that:
$user = ''; // (Not an empty strings just removed the part that grabs this for security reasons)
$password = ''; // (Not an empty strings just removed the part that grabs this for security reasons)
$password = crypt($password, base64_encode($password));
$line = $user . ':' . $password;
$fh = fopen('path/to/password/file','a');
fwrite($fh,$line);
fclose($fh);
However, I'd like to remove any current entry on that person. If I knew what their old password was, for example pass123
, I'd just use str_replace($user . ':' . 'pass123','',$currentfiledata)
, but I don't, and I have no way to store it.
How would I be able to do this? I assume using preg_replace
or something similar, but I'm not experienced with that.