HTTP 身份验证在 Linux 托管网站中不起作用
我有一个Linux托管的网站,我尝试使用下面的代码进行http身份验证来访问一个机密页面,但它似乎不起作用,每次我输入用户名和密码时,身份验证框都会再次弹出!谁能告诉我哪里出了问题?
<?php
// User name and password for authentication
$username='username';
$password='password';
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
($_SERVER['PHP_AUTH_USER'] !=$username) || ($_SERVER['PHP_AUTH_PW'] !=$password))
{
// The user name/password are incorrect so send the authentication headers
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="My Web Page:The Freedom Square Of Internet"');
exit('<h2>My Web page</h2>Sorry, you must enter a valid user name and password to access this page.');
}
?>
I have a linux hosted website, i am trying to use the below code for http-authentication for access to one confidential page, but it doesn't seem to work, everytime i enter username and password, the authentication box pops up again! Can anybody tell me where m i going wrong?
<?php
// User name and password for authentication
$username='username';
$password='password';
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
($_SERVER['PHP_AUTH_USER'] !=$username) || ($_SERVER['PHP_AUTH_PW'] !=$password))
{
// The user name/password are incorrect so send the authentication headers
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="My Web Page:The Freedom Square Of Internet"');
exit('<h2>My Web page</h2>Sorry, you must enter a valid user name and password to access this page.');
}
?>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的服务器很可能“吃掉”“Authorization”标头,吐出
$_SERVER
的内容可以帮助您更好地了解正在发生的情况。您使用什么服务器?如果您使用 fastcgi 并且以上是您的问题,那么这是 Apache 的
mod_rewrite
解决方案:http://search.cpan.org/~mramberg/Catalyst-Runtime-5.80012/lib/Catalyst/Engine/FastCGI.pm#Authorization_header_with_mod_fastcgi_or_mod_cgi
然后,您可以非常简单地处理授权标头,如果PHP 不会为你做这件事。
It's quite possible that your server 'eats' the 'Authorization' header, spitting out the contents of
$_SERVER
can help you get a better idea of what's going on. What server are you using?Here's a
mod_rewrite
solution for Apache if you're using fastcgi and the above is your issue:http://search.cpan.org/~mramberg/Catalyst-Runtime-5.80012/lib/Catalyst/Engine/FastCGI.pm#Authorization_header_with_mod_fastcgi_or_mod_cgi
You can then quite simply process the Authorization header if PHP doesn't do it for you.