beoliver
beoliver

Reputation: 5759

php can not authorize

Trying to implement a very simple protected area. I have two php files:

The first: 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'); 
  }
?>

and the second: hello.php

<?php

require_once('secure.php');

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

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

</html>

when visiting hello.php I am prompted for username / password as expected. But what I did not expect was constantly be denied access.

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.

both chrome and safari. Is there something obvious that I am missing?

Upvotes: 1

Views: 2313

Answers (1)

pilsetnieks
pilsetnieks

Reputation: 10420

The code works fine for me, so it's probably a server configuration issue, perhaps this one - PHP_AUTH_USER not set?

Upvotes: 1

Related Questions