使用 PHP 8.0 的身份验证-使用 .htaccess 和 .htpasswd 保护文件和文件夹的密码

Authentication with PHP 8.0-Password protecting files and folders with .htaccess and .htpasswd

我正在尝试使用密码保护我的 Apache 目录中的某些文件夹和文件。我将 .htaccess 文件放在该文件夹中,当我转到该目录时,浏览器要求进行身份验证。

我尝试使用命令 htpasswd -c .htpasswd client 从命令行创建密码。 客户端用户名已经存在于我的.htpasswd 文件中,但它没有提示我生成新密码。

我的 .htaccess 文件如下所示:

AuthUserFile C:/Apache24/htdocs/PHPandMySQL-2020/.htpasswd
AuthType Basic
AuthName "My Protected Folder"
Require valid-user

我的 .htpasswd 看起来像这样:

client: f.i9PC3.AtcXE

身份验证适用于文件夹。但是当我转到命令行并键入:htpasswd -c .htpasswd client 时。它没有提示我生成新密码。

有人可以帮我吗?

It says it doesn't recognize the command.

它不会提示输入密码,因为您实际上不是 运行 命令! htpasswd.exe 可执行文件通常安装在 Apache 安装的 bin 子目录中。因此,如果它位于 C:\Apache24\bin\htpasswd.exe,那么您需要在相应目录的命令提示符处使用以下命令。

> C:\Apache24\bin\htpasswd -c .htpasswd client

或者,导航到 bin 目录并在命令行上将完整路径传递到 .htpasswd 文件。

或者,add the bin directory to the Windows PATH environment variable 以避免在命令中输入完整的 file-path。