HTTP authentication not working when switched to LIVE host
Hey guys, I'm stuck again, so where else to go than stackoverflow? So heres the deal: My site is set up and works fine on LOCALHOST. I have an admin section of my site - nothing too fancy, but have used basic http authentication to protect it. I have just FTP'd the whole lot over to the LIVE HOST directory and low and behold the HTTP authentication doesn't work. Specifically, the username/password pop-up box appears with the correct details on it. However, when I type in the correct login details the box doesn't let me on the page, it just reappears开发者_运维百科 empty. The authentication script is a separate file:
<?php
// User name and password for authentication
$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)) {
// The user name/password are incorrect so send the authentication headers
header('HTTP/1.1 401 Unauthorized');
header('WWW-Authenticate: Basic realm="The Manor Cheadle"');
exit('<h2>The Manor Cheadle</h2>Sorry, you must enter a valid user name and
password to access this page.');
}
?>
and is referenced at the top of every page:
<?php
require_once('authorize.php');
?>
Any ideas how to get round this and get it to work?
Thanks in advance
I always used this for simple authentication, never had problems with it. But it looks very similar to yours.
<?
$auth = 0;
if (!isset($_SERVER['PHP_AUTH_USER']))
{
header('WWW-Authenticate: Basic realm="Login"');
header('HTTP/1.0 401 Unauthorized');
print "you need to login";
exit;
}
else
{
if ($_SERVER['PHP_AUTH_USER'] == "UserName" and $_SERVER['PHP_AUTH_PW'] == "Password")
{
$auth=1;
}
else
{
header('WWW-Authenticate: Basic realm="Login"');
header('HTTP/1.0 401 Unauthorized');
print "you need to login";
exit;
}
}
if ($auth==0) {
print "you need to login";
exit;
}
?>
精彩评论