I had a question about the use of the HTTP header 'WWW-Authenticate'
in PHP scripts. For example, the script below sends the header 'WWW-
Authenticate: Basic Realm="Secret Stash"', followed by the header
'HTTP/1.0 401 unauthorized', to force the web browser to display a
username/password dialog. The script then calls exit().
I don't understand how the script gets re-invoked (after the username
and password have been supplied in the dialog box and user has clicked
OK)
because the script called exit() after issuing the two header() calls.
I understand that once the username and password have been supplied
that $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] set.
But how does the server know to re-invoke the same script a second
time? After all the script just did an exit() after sending the
headers.
<?php
// Preset authentication status to false.
$authorized = FALSE;
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW'])) {
// Read the authentication file into an array
$authFile = file("./authenticationF ile.txt");
// Cycle through each line in file, searching for
authentication match.
foreach ($authFile as $login) {
list($username, $password) = explode(":", $login);
// Remove the newline from the password
$password = trim($password) ;
if ($username == $_SERVER['PHP_AUTH_USER'] &&
$password == md5($_SERVER['PHP_AUTH_PW'])) {
$authorized = TRUE;
break;
}
}
}
// If not authorized, display authentication prompt or 401 error
if (! $authorized) {
header('WWW-Authenticate: Basic Realm="Secret Stash"');
header('HTTP/1.0 401 Unauthorized');
print('You must provide the proper credentials! Buster!!!');
exit;
}
// restricted material goes here...
?>
in PHP scripts. For example, the script below sends the header 'WWW-
Authenticate: Basic Realm="Secret Stash"', followed by the header
'HTTP/1.0 401 unauthorized', to force the web browser to display a
username/password dialog. The script then calls exit().
I don't understand how the script gets re-invoked (after the username
and password have been supplied in the dialog box and user has clicked
OK)
because the script called exit() after issuing the two header() calls.
I understand that once the username and password have been supplied
that $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] set.
But how does the server know to re-invoke the same script a second
time? After all the script just did an exit() after sending the
headers.
<?php
// Preset authentication status to false.
$authorized = FALSE;
if (isset($_SERVER['PHP_AUTH_USER']) &&
isset($_SERVER['PHP_AUTH_PW'])) {
// Read the authentication file into an array
$authFile = file("./authenticationF ile.txt");
// Cycle through each line in file, searching for
authentication match.
foreach ($authFile as $login) {
list($username, $password) = explode(":", $login);
// Remove the newline from the password
$password = trim($password) ;
if ($username == $_SERVER['PHP_AUTH_USER'] &&
$password == md5($_SERVER['PHP_AUTH_PW'])) {
$authorized = TRUE;
break;
}
}
}
// If not authorized, display authentication prompt or 401 error
if (! $authorized) {
header('WWW-Authenticate: Basic Realm="Secret Stash"');
header('HTTP/1.0 401 Unauthorized');
print('You must provide the proper credentials! Buster!!!');
exit;
}
// restricted material goes here...
?>
Comment