Skip to content
PHP Logo

What are the differences between using PHP for password prompts versus other methods like .htaccess?

When using PHP for password prompts, the authentication process is handled within the PHP script itself, giving more flexibility and customization options. On the other hand, using .htaccess for password prompts relies on server configuration files and may be simpler to set up for basic password protection.

php
<?php
$username = 'admin';
$password = 'password';

if (!isset($_SERVER['PHP_AUTH_USER']) || $_SERVER['PHP_AUTH_USER'] != $username || $_SERVER['PHP_AUTH_PW'] != $password) {
    header('WWW-Authenticate: Basic realm="Restricted Area"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Access Denied';
    exit;
}

echo 'You are logged in!';
?>