What are the potential security risks of using .htaccess for password protection in PHP projects?
Using .htaccess for password protection in PHP projects can pose security risks because it relies on server configuration files that may be accessible or misconfigured. A more secure approach is to handle password protection within the PHP code itself, using proper encryption techniques and secure storage methods.
<?php
$username = 'admin';
$password = 'password123';
if ($_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;
}
// Rest of your code goes here