Simple authenticate
suggest changeSend these headers before any output. Otherwise header() does not run. PHP_AUTH_USER and PHP_AUTH_PW are filled when PHP runs as an Apache module (and some other SAPIs). They stay empty under CGI. This example prints the password.
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Text to send if user hits Cancel button';
exit;
}
echo "<p>Hello {$_SERVER['PHP_AUTH_USER']}.</p>";
$user = $_SERVER['PHP_AUTH_USER']; // Let's save the information
echo "<p>You entered {$_SERVER['PHP_AUTH_PW']} as your password.</p>";
$pass = $_SERVER['PHP_AUTH_PW']; // Save the password (optionally add encryption)!
?>
Your HTML page follows the closing ?>.
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents