What are the potential security risks of using hotlinked images with PHP?

When using hotlinked images with PHP, there is a potential security risk known as "image hotlinking." This occurs when a website directly links to images hosted on another server without permission, leading to increased bandwidth usage and potential copyright infringement. To prevent image hotlinking, you can check the HTTP referer header to ensure that the request is coming from your own domain.

<?php
$referer = $_SERVER['HTTP_REFERER'];

if(!empty($referer) && strpos($referer, 'yourdomain.com') === false) {
    // Redirect to a default image or display an error message
    header('Location: default_image.jpg');
    exit;
}

// Display the hotlinked image
$image = 'hotlinked_image.jpg';
header('Content-Type: image/jpeg');
readfile($image);
?>