1

试图实现一个非常简单的保护区。我有两个 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.

铬和野生动物园。我有什么明显的遗漏吗?

4

1 回答 1

1

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

于 2013-02-10T17:43:42.763 回答