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
```>

    You have to start by addressing the first warning: Undefined array key "HTTP_AUTHORIZATION".

    Depending on a bunch of things (such as the underlying web server being used*), you may need to look for something else other than that specific element of the $_SERVER array. If this is another exercise from some book or tutorial, I might move on to something else, as I believe usage of HTTP Authorization is pretty rare these days. (Someone correct me if I'm wrong?)


    * Are you, in fact, testing this with a web server (as opposed to just calling the script directly on your computer via PHP)?

    I have Fedora 38 with PHP Version 8.2.6
    but I reading one book it is with php 7, the exercises are in php 7

    I have the code and workstation in the same computer
    I just calling the script directly on my computer via PHP

    if it doesn't work it doesn't matter
    It is not used in the future

    thank you
    you always help me

    Write a Reply...