What are the potential risks of shops linking directly to images on a website and how can this be prevented using PHP?
When shops directly link to images on a website, it can lead to increased bandwidth usage and potential hotlinking, where other websites use the image without permission. To prevent this, you can check the HTTP referer header in PHP to ensure that the request is coming from an allowed domain.
<?php
$allowed_domains = array('example.com', 'shop.com');
$referer = $_SERVER['HTTP_REFERER'];
$referer_host = parse_url($referer, PHP_URL_HOST);
if (!in_array($referer_host, $allowed_domains)) {
// Redirect to a default image or show an error message
header('Location: default_image.jpg');
exit;
}
// Serve the requested image
$image_path = 'images/' . $_GET['image'];
header('Content-Type: image/jpeg');
readfile($image_path);
?>
Related Questions
- What are the potential security risks associated with using the isset() function in PHP for login forms?
- What are some common techniques for improving the efficiency of search functions in PHP applications that interact with MySQL databases?
- What is the correct way to assign a value to a textarea in PHP?