Protect files from direct download
Saturday, June 16th, 2007Hi guys,
With this script you can control the access to your files with a ACL from secures referrers.
Here the code:
//Array with valid referers.
-
$validos = Array
-
(
-
"http://www.domain.com/files.php",
-
"http://www.domain.com/forum.php",
-
"http://www.domain.com/download/file.php"
-
};
-
-
//If is the referer in the array, we open the file
-
if (in_array($_SERVER["HTTP_REFERER"],$validos))
-
{
-
//Open the file
-
$file = $_SERVER["DOCUMENT_ROOT"]."/somefile.rar";
-
//Send the header with the content type of the file
-
header("Content-type: application/x-rar-compressed;");
-
//Send the header with the length of the file
-
header('Content-Length: ' . filesize($file));
-
//Send the header with the name of the file
-
header('Content-Disposition: attachment; filename= somefile.rar');
-
//Put the content in the var
-
readfile($file);
-
}
-
else
-
//we redirect the user to another page.
-
header("Location:http://www.domain.com/notpermision.php");
Posted in Php, tip | No Comments »

