Home   /   DKMIS Blog   /   Codes

Password Protection in PHP

Posted on 31 January 2025 12:14 pm

If you're building with PHP, you can also add basic authentication directly in your script:

$valid_username = 'username';
$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="Restricted Area"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Access denied';
    exit;
}
?>

Replace username and password with your credentials.