What are some potential issues with using .htaccess files to restrict access to images from external domains in PHP?

Potential issues with using .htaccess files to restrict access to images from external domains in PHP include the fact that .htaccess files are specific to Apache servers and may not work on all hosting environments. Additionally, using .htaccess files can be cumbersome to manage and may not provide the flexibility needed for more complex access control requirements. To solve this issue, a more robust solution using PHP code to check the referrer header before serving the image can be implemented.

<?php
$allowed_domains = array('example.com', 'subdomain.example.com');
$referer = isset($_SERVER['HTTP_REFERER']) ? parse_url($_SERVER['HTTP_REFERER'], PHP_URL_HOST) : '';

if(!in_array($referer, $allowed_domains)) {
    header('HTTP/1.0 403 Forbidden');
    exit;
}

// Serve the image here