doingword.com

Protect files from direct download

June 16th, 2007

Hi 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.
  1. $validos = Array
  2. (
  3. "http://www.domain.com/files.php",
  4. "http://www.domain.com/forum.php",
  5. "http://www.domain.com/download/file.php"
  6. };
  7.  
  8. //If is the referer in the array, we open the file
  9. if (in_array($_SERVER["HTTP_REFERER"],$validos))
  10. {
  11. //Open the file
  12. $file = $_SERVER["DOCUMENT_ROOT"]."/somefile.rar";
  13. //Send the header with the content type of the file
  14. header("Content-type: application/x-rar-compressed;");
  15. //Send the header with the length of the file
  16. header('Content-Length: ' . filesize($file));
  17. //Send the header with the name of the file
  18. header('Content-Disposition: attachment; filename= somefile.rar');
  19. //Put the content in the var
  20. readfile($file);
  21. }
  22. else
  23. //we redirect the user to another page.
  24. header("Location:http://www.domain.com/notpermision.php");
[Slashdot] [Digg] [Reddit] [del.icio.us] [Facebook] [Technorati] [Google] [StumbleUpon]

Entry Filed under: Php, tip


Leave a Comment

Required

Required, hidden

Some HTML allowed:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Trackback this post  |  Subscribe to the comments via RSS Feed