How can PHP be used to prevent image theft on a website?
To prevent image theft on a website, you can use PHP to disable right-clicking on images, add a watermark to images, or block direct access to image files. One common method is to use PHP to check the HTTP referer when an image is requested, ensuring that it is coming from the intended website and not hotlinked or embedded elsewhere.
<?php
// Check HTTP referer to prevent hotlinking
$referer = $_SERVER['HTTP_REFERER'];
$allowed_domain = 'yourwebsite.com';
if(!strstr($referer, $allowed_domain)){
// Redirect or display an error message
header('Location: error.php');
exit;
}
// Serve the image file
$image_path = 'path/to/your/image.jpg';
header('Content-Type: image/jpeg');
readfile($image_path);
?>