php无法授权

时间:2013-02-10 17:08:33

标签: php authentication

尝试实施一个非常简单的保护区域。我有两个php文件:

第一个:secure.php

<?php
  $username = 'user';
  $password = 'password';

  if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) ||
    ($_SERVER['PHP_AUTH_USER'] != $username) || ($_SERVER['PHP_AUTH_PW'] != $password)) {
    header('HTTP/1.1 401 Unauthorized');
    header('WWW-Authenticate: Basic realm="Secure Area"');
    exit('You are not authorized'); 
  }
?>

和第二个:hello.php

<?php

require_once('secure.php');

?>
<doctype! html>
<head>
</head>

<body>
<p>hello</p>
</body>

</html>

访问hello.php时,系统会提示我输入用户名/密码。但我没想到的是经常被拒绝访问。

You are not authorized
Authorization Required

This server could not verify that you are authorized to access the document requested. Either you supplied the wrong credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.

铬和野生动物园。有什么明显的东西我不见了吗?

1 个答案:

答案 0 :(得分:1)

代码对我来说很好,所以它可能是服务器配置问题,也许是这个问题 - PHP_AUTH_USER not set?