How can PHP be used to restrict access to downloadable files based on the referring URL?
To restrict access to downloadable files based on the referring URL, you can use PHP to check the HTTP referer header and only allow access if it matches a specific URL. This can help prevent unauthorized access to your files by ensuring that they are only accessed from approved sources.
<?php
$allowed_referer = 'https://www.example.com';
if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] == $allowed_referer) {
// Serve the downloadable file
$file = 'path/to/downloadable/file.pdf';
header('Content-Type: application/pdf');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
readfile($file);
} else {
// Redirect or display an error message
echo 'Access denied.';
}
?>
Related Questions
- What are the potential pitfalls of using unnecessary variables like $text in PHP?
- Are there any alternative methods to using JavaScript for automatically submitting preselected radio buttons in PHP forms?
- What are some resources or forums that can provide additional help with PHP session management?