How can PHP be used to prevent direct downloading of mp3 files from a URL?
To prevent direct downloading of mp3 files from a URL, you can use PHP to check the referrer of the request. By checking if the request is coming from your website, you can allow access to the mp3 file. If the request is coming from a different domain or directly, you can redirect the user or deny access to the file.
<?php
$referrer = $_SERVER['HTTP_REFERER'];
if($referrer && strpos($referrer, 'yourwebsite.com') !== false) {
// Allow access to the mp3 file
header('Content-Type: audio/mpeg');
readfile('yourfile.mp3');
} else {
// Redirect or deny access
header('Location: error_page.php');
exit();
}
?>