如何在 PHP 中使用基本 HTTP 身份验证?
我正在尝试使用 基本 HTTP 身份验证 并按照 PHP 手册页上的示例进行操作。但这对我不起作用。变量 $_SERVER['PHP_AUTH_USER']
似乎未设置。当用户尝试登录时,系统会提示用户使用新的登录对话框。服务器正在运行 PHP 5.3.3,我已尝试使用 Google Chrome 和 Internet Explorer。
这是我使用的简单示例:
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Jonas Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'User pressed Cancel';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as you password.</p>";
}
?>
出了什么问题?如何在 PHP 中使用基本 HTTP 身份验证?
I'm trying to use Basic HTTP Authentication and followed the example on the PHP manual page. But it doesn't work for me. The variable $_SERVER['PHP_AUTH_USER']
doesn't seem to be set. When a user try to log in, the user is prompted whith a new login-dialog. The server is running PHP 5.3.3 and I have tried with Google Chrome and Internet Explorer.
Here is the simple example that I used:
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="Jonas Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'User pressed Cancel';
exit;
} else {
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as you password.</p>";
}
?>
What is wrong? How can I use Basic HTTP Authentication in PHP?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
试试这个::
Try this::
对于 PHP-CGI:
在 .htaccess 中添加以下内容:
并在脚本的开头添加以下内容:
For PHP-CGI:
in .htaccess add this:
and at the beginning of your script add this:
PHP 是如何运行的?如果是通过Apache mod_cgi,恐怕你根本无法掌握认证信息。 Apache 不会将其传递给 CGI 应用程序,除非您使用 SECURITY_HOLE_PASS_AUTHORIZATION 标志进行编译。 (是否确实存在安全漏洞取决于您服务器上的其他用户是否比您的网站用户具有更低或更高的权限。)
如果是 IIS CGI,则必须确保仅配置“匿名”目录访问,或者 IIS将尝试介入并自行验证凭据。
HTML 注入安全漏洞(好吧,如果它有效的话)。使用
htmlspecialchars()
。伙计,那个 PHP 文档页面充满了高度可疑的建议和代码。您也可以尝试查看
$_SERVER['HTTP_AUTHORIZATION']
,尽管我怀疑这是 mod_cgi 问题,在这种情况下,这两个变量都不会出现,您将不得不诉诸基于 cookie 的登录。或者更改为采用更现代的 PHP 托管方法的主机,例如 FastCGI 或 mod_php。How is PHP being run? If it's through Apache mod_cgi, I'm afraid you cannot get hold of the authentication information at all. Apache won't pass it to CGI apps unless you compile it with the
SECURITY_HOLE_PASS_AUTHORIZATION
flag. (Whether it's actually a security hole or not depends on whether other users on your server have a lower or greater privilege than your website users.)If it's IIS CGI, you have to ensure that only ‘Anonymous’ directory access is configured, or IIS will try to step in and authenticate credentials itself.
HTML-injection security hole (well, if it worked). Use
htmlspecialchars()
. Man, is that PHP doc page full of highly questionable advice and code.You can try to look at
$_SERVER['HTTP_AUTHORIZATION']
as well, though I suspect it's the mod_cgi issue in which case neither variable will be present and you will have to resort to cookie-based login instead. Or change to a host with a more modern approach to PHP hosting, such as FastCGI or mod_php.我认为 PHP 方法依赖于 Web 服务器上配置的基本身份验证。执行此操作的一个简单方法是将 .htaccess 文件包含在要为其启用基本身份验证的目录中。
大多数基于 UNIX 的 Web 主机允许您通过上传此文件来完成此操作。一个单独的文件(称为 .htauth)将保存允许访问站点或目录的用户登录名。
I think the PHP methods rely on Basic Authentication being configured on the Web Server. An easy way to do this is to include a .htaccess file in the directory you want to enable basic authentication for.
Most unix-based web hosts allow you to do this by just uploading this file. A separate file (call this .htauth) will hold the user logins that are allowed to access the site or directory.
检查您在尝试访问两个变量之前是否覆盖了变量
$_SERVER['PHP_AUTH_USER']
和$_SERVER['PHP_AUTH_PW']
。如果上述情况并非如此,请检查您是否使用 php 作为 cgi mod。如果您使用 php 作为 cgi mod,那么在大多数情况下您将不会得到
$_SERVER['PHP_AUTH_USER']
和$_SERVER['PHP_AUTH_PW']
因为通常我们不要使用选项 SECURITY_HOLE_PASS_AUTHORIZATION 编译 apache因此,如果您想获取这两个变量,请使用选项 SECURITY_HOLE_PASS_AUTHORIZATION 重新编译 apache,或者您可以使用 .htaccess 方法。
Check if you have overrided your variable
$_SERVER[‘PHP_AUTH_USER’]
and$_SERVER[‘PHP_AUTH_PW’]
before the place where you are trying to access the both variables.If above is not the case then chek if you are using php as cgi mod or not. IF you are using php as cgi mod then in the most of case you will not get
$_SERVER[‘PHP_AUTH_USER’]
and$_SERVER[‘PHP_AUTH_PW’]
because generraly we do not compile the apache with optionSECURITY_HOLE_PASS_AUTHORIZATION
So if you want to get both variable then re-compile apache with option SECURITY_HOLE_PASS_AUTHORIZATION or you can use .htaccess approach.
如果您的 $_SERVER 变量中没有 PHP_AUTH_USER 和 PHP_AUTH_PW
您需要按照此处所述检查PHP_AUTH_DIGEST: https://www.php.net/manual/en/features.http-auth.php
检查示例#2。
If you don't have PHP_AUTH_USER and PHP_AUTH_PW in your $_SERVER variables
you need to check PHP_AUTH_DIGEST as described here: https://www.php.net/manual/en/features.http-auth.php
Check example #2.