What is the purpose of using WWW-Authenticate in PHP for securing an admin area on a website?
Using WWW-Authenticate in PHP allows for basic authentication to be implemented, which prompts users to enter a username and password before accessing a specific area of a website, such as an admin area. This helps secure the admin area by restricting access only to authorized users with valid credentials.
<?php
$valid_username = 'admin';
$valid_password = 'password';
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW']) || $_SERVER['PHP_AUTH_USER'] != $valid_username || $_SERVER['PHP_AUTH_PW'] != $valid_password) {
header('WWW-Authenticate: Basic realm="Admin Area"');
header('HTTP/1.0 401 Unauthorized');
echo 'Access Denied';
exit;
}
// Admin area code here
?>