如何进行Web身份验证

时间:2014-07-04 14:14:23

标签: php http

我有问题。 HTTP身份验证不允许我在我的网站上拥有正确的密码和用户名。我的意思是它仍然会弹出并询问我的凭据。我在代码中错误的地方或我应该检查别的东西?对不起,我的英语非常糟糕。非常感谢你。

CODE:

<?php
   $username='xxx';
   $password='xxx_XXX';
   if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])
    || $_SERVER['PHP_AUTH_USER'] != $username || $_SERVER['PHP_AUTH_PW'] != $password){
    header('HTTP/1.0 401 Unauthorized');
    header('WWW-Authenticate: Basic realm="Website"');
    exit;
}
?>

1 个答案:

答案 0 :(得分:0)

使用htaccess(假设为apache)

将此问题发布到您的Web服务器职责中
<Directory "/var/www/html/protected">
  AuthType Basic
  AuthName "Authentication Required"
  AuthUserFile "/etc/htpasswd/.htpasswd"
  Require valid-user

  Order allow,deny
  Allow from all
</Directory>

然后使用以下命令创建htauth文件:     htpasswd -c /etc/htpasswd/.htpasswd user1     htpasswd /etc/htpasswd/.htpasswd user2

似乎是一个更好的工具,除非您正在使用功能齐全的登录网站 - 但我认为您的代码可能需要在此之前成熟; - )