HTTP 身份验证在 Linux 托管网站中不起作用

发布于 2024-12-11 23:21:35 字数 719 浏览 0 评论 0原文

我有一个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

微凉 2024-12-18 23:21:35

您的服务器很可能“吃掉”“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

RewriteCond %{HTTP:Authorization} ^(.+)
RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]

然后,您可以非常简单地处理授权标头,如果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

RewriteCond %{HTTP:Authorization} ^(.+)
RewriteRule ^(.*)$ $1 [E=HTTP_AUTHORIZATION:%1,PT]

You can then quite simply process the Authorization header if PHP doesn't do it for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文