Reputation: 268
I am using the following PHP Authentication script for a site, but when I enter the user and password, the script won't accept them as correct (TEST/TEST1) and it always reloads the user/pass prompt (login function).
Here is my script.
function login()
{
header('WWW-Authenticate: Basic realm="Acceso restringido."');
header('HTTP/1.0 401 Unauthorized');
echo "Acceso restringido.\n";
exit;
}
if (!isset($_SERVER['PHP_AUTH_USER'])) {
login();
}
list($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) = explode(':' , base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'], 6)));
// open a user/pass prompt
if (!isset($_SERVER['PHP_AUTH_USER'])) {
login();
} else {
if ($_SERVER['PHP_AUTH_USER'] == 'TEST' && $_SERVER['PHP_AUTH_PW'] == 'TEST1') {
} else {
login();
}
}
Upvotes: 0
Views: 998
Reputation: 2051
You can try this, it works fine for me:
session_start();
function login() {
header("WWW-Authenticate: Basic");
header("HTTP/1.0 401 Unauthorized");
die;
}
if (!isset($_SESSION["AUTH_SUCCESS"])) {
$_SESSION["AUTH_SUCCESS"] = 0;
}
if ($_SESSION["AUTH_SUCCESS"] == 0) {
$user = "TEST";
$pass = "TESTPASS";
if ($_SERVER["PHP_AUTH_USER"] != $user || $_SERVER["PHP_AUTH_PW"] != $pass) {
login();
} else {
$_SESSION["AUTH_SUCCESS"] = 1;
}
}
if ($_SESSION["AUTH_SUCCESS"] == 0) {
die("You have entered a wrong password/username.");
}
This code holds a variable in session named AUTH_SUCCESS, as long this variable is 0 (Zero) then the prompt will appear, when the user enters the user/pass, then the variable value will become 1 and the prompt will stop appearing.
Upvotes: 1