Warning: Undefined array key "HTTP_AUTHORIZATION" in /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php on line 4
Deprecated: substr(): Passing null to parameter #1 ($string) of type string is deprecated in /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php on line 4
Warning: Undefined array key "PHP_AUTH_USER" in /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php on line 10
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php:4) in /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php on line 14
Warning: Cannot modify header information - headers already sent by (output started at /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php:4) in /var/www/html/php-books/php-and-mysql-web-development/chapter16/using-basich-authentication-in-php-372/basic-auth.php on line 15
<?php
if ((!isset($_SERVER['PHP_AUTH_USER'])) &&
(!isset($_SERVER['PHP_AUTH_PW'])) &&
(substr($_SERVER['HTTP_AUTHORIZATION'],0,6) == 'Basic'))
{
list($_SERVER['PHP_AUTH_USER'],$_SERVER['PHP_AUTH_PW']) =
explode (':', base64_decode(substr($_SERVER['HTTP_AUTHORIZATION'],6)));
}
//Replace this if statement with a database query or similar
if(($_SERVER['PHP_AUTH_USER'] != 'user') ||
($_SERVER['PHP_AUTH_PW'] != 'pass')) {
//visitor has not yet given details, or their
//name and password combination are not correct
header('WWW-Authenticate: Basic realm="Realm-Name"');
header('HTTP/1.0 401 Unauthorized');
} else {
?>
<!DOCTYPE html>
<html>
<head>
<title>Secre Page</title>
</head>
<body>
<?php
echo '<h1>Here it is!</h1>
<p>I bet you are glad you can see this secret page.</p>';
}
?>
</body>
</html
```>