How can PHP be used in conjunction with .htaccess for user authentication?

To use PHP in conjunction with .htaccess for user authentication, you can create a PHP script that checks the user's credentials and then use mod_rewrite rules in the .htaccess file to redirect requests to this script for authentication. The PHP script can verify the user's credentials against a database or any other authentication method, and then set a session variable to indicate that the user is authenticated.

<?php
session_start();

if(!isset($_SESSION['authenticated']) || $_SESSION['authenticated'] !== true) {
    header('HTTP/1.1 401 Unauthorized');
    exit;
}
?>
```

Add the following mod_rewrite rules to your .htaccess file to redirect requests to the PHP authentication script:

```apache
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ authenticate.php [L]