How can htpasswd be used as a simple solution for protecting a website with a password in PHP?

To protect a website with a password in PHP, you can use htpasswd to create a password file with encrypted passwords. This file can then be used in conjunction with a .htaccess file to prompt users for a password before accessing the website.

<?php
$htpasswd_file = '/path/to/.htpasswd';
$htpasswd_user = 'username';
$htpasswd_pass = 'password';

$encrypted_pass = password_hash($htpasswd_pass, PASSWORD_BCRYPT);

file_put_contents($htpasswd_file, $htpasswd_user . ':' . $encrypted_pass);
?>