What are some techniques for preventing video downloads using PHP?
To prevent video downloads using PHP, one common technique is to use a combination of server-side validation and HTTP headers. By setting certain headers in the response, you can prevent the browser from caching or saving the video file locally. Additionally, you can check the referrer of the request to ensure that the video is being accessed from an authorized source.
<?php
// Check if the request is coming from an authorized source
$referrer = $_SERVER['HTTP_REFERER'];
if($referrer !== 'https://www.yourwebsite.com/video-page.php') {
header('HTTP/1.0 403 Forbidden');
exit;
}
// Set headers to prevent caching and downloading
header('Cache-Control: no-cache, no-store, must-revalidate');
header('Expires: 0');
header('Pragma: no-cache');
// Output the video file
$videoPath = 'path/to/your/video.mp4';
header('Content-Type: video/mp4');
readfile($videoPath);
?>