Is it possible to achieve the desired restriction of image access using PHP scripts instead of .htaccess files?

To achieve the desired restriction of image access using PHP scripts instead of .htaccess files, you can create a PHP script that checks the referring URL of the request and only serves the image if the referring URL matches a specific domain. This way, you can control access to the images based on the referring URL.

<?php
$allowed_domain = 'https://www.example.com';

$referer = $_SERVER['HTTP_REFERER'];

if(strpos($referer, $allowed_domain) === false) {
    header("HTTP/1.0 403 Forbidden");
    exit;
}

$image_path = 'path/to/image.jpg';
header('Content-Type: image/jpeg');
readfile($image_path);