What are some common methods to prevent direct access to images in PHP websites?
Direct access to images in PHP websites can be prevented by using a .htaccess file to restrict access to the images directory, or by using PHP to check if the request is coming from a valid source before serving the image.
// Prevent direct access to images using PHP
if(!isset($_SERVER['HTTP_REFERER'])) {
header('HTTP/1.0 403 Forbidden');
exit;
}
// Serve the image if the request is coming from a valid source
$imagePath = 'path/to/image.jpg';
header('Content-Type: image/jpeg');
readfile($imagePath);
Related Questions
- What are common issues that may prevent a PHP newsletter script from sending emails successfully?
- Are there any other methods or techniques to restrict access to scripts run via Cron jobs in PHP, apart from those mentioned in the forum thread?
- How can PHP beginners effectively integrate shell commands into their PHP scripts for server management tasks?