How can header manipulation be used to achieve automatic htaccess login in PHP?

To achieve automatic htaccess login in PHP, header manipulation can be used to send the necessary Authorization header with the correct credentials. This can be done by setting the Authorization header with the base64 encoded username and password before making any requests to the protected resource.

<?php
$username = 'your_username';
$password = 'your_password';

$credentials = base64_encode("$username:$password");

$url = 'https://example.com/protected/resource';
$headers = [
    'Authorization: Basic ' . $credentials
];

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);

// Process the result here

curl_close($ch);
?>