删除字符串PHP的未知部分

时间:2015-10-30 18:50:44

标签: php .htpasswd

我正在编写一个更新.htpasswd文件的PHP脚本(是的,我知道它不是很安全,但我有一些保护措施可以阻止人们访问脚本)。

现在,我只是附加当前文件,如:

    $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);

但是,我想删除该人当前的任何条目。如果我知道他们的旧密码是什么,例如pass123,我只会使用str_replace($user . ':' . 'pass123','',$currentfiledata),但我没有,而且我无法存储它。

我怎么能这样做?我假设使用preg_replace或类似的东西,但我对此没有经验。

1 个答案:

答案 0 :(得分:1)

这不是执行密码更新的理智方式。但既然你已经知道了,我就不会就安全问题向你讲课。

如果您必须这样做,您可以使用preg_replace()执行替换,如下所示:

^

上面的语句搜索以给定用户名开头的任何行(由.*声明},后跟冒号,然后是密码(由{{1}}表示 - 这意味着任何字符,重复零次或多次)。匹配项将替换为空字符串。