What are the potential pitfalls of using basic authentication for page protection in PHP?
Using basic authentication for page protection in PHP can be insecure as the username and password are sent in plaintext, making it vulnerable to interception. To improve security, it is recommended to use HTTPS to encrypt the communication between the client and server when using basic authentication.
<?php
if (!isset($_SERVER['PHP_AUTH_USER'])) {
header('WWW-Authenticate: Basic realm="My Realm"');
header('HTTP/1.0 401 Unauthorized');
echo 'Unauthorized access';
exit;
} else {
// Check username and password here
}
?>