How can PHP be used to automatically log in to an htaccess-protected folder without the user's knowledge?

To automatically log in to an htaccess-protected folder without the user's knowledge, you can use PHP to send the authentication credentials in the HTTP request headers. This can be achieved by setting the 'Authorization' header with the encoded username and password. By doing this, the user will not see the login prompt and will be automatically authenticated.

<?php
$username = 'username';
$password = 'password';
$url = 'https://example.com/protected-folder';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Basic ' . base64_encode("$username:$password")));

$response = curl_exec($ch);
curl_close($ch);

echo $response;
?>