views:

80

answers:

1

It's a common problem, solved many times, but for some reason i cannot find the right information.

Some constraints and requirements:

  • Technique for LAMP stack.
  • Fine-grained control of which files are accessible.
  • No basic authentication with htpasswd files, i want to provide a custom login frontend.
  • Should be able to securely protect and serve big video files

How do all those sites protect their files from public without using basic authentication?

Thanks!

A similar question: http://stackoverflow.com/questions/1688568/performance-oriented-way-to-protect-files-on-php-level

+4  A: 

You would usually redirect any requests for the media files to a PHP script. The PHP script does the login authentication, and on success, loads the requested media file from a protected location, and passes it through to the browser, for example using fpassthru() or fread().

You can set up a very elegant solution using a set of mod_rewrite instructions, for example rewriting

www.example.com/media/music.mp3

internally to

www.example.com/media/index.php?file=music.mp3

the method is not cheap, as the PHP interpreter has to be started for every download, and pass through every byte of the file. For a discussion of possible alternatives, I asked a question about that a few months back: Performance-oriented way to protect files on PHP level?

Pekka
That's exactly how it's done. Note : the protected location is a folder outside of your apache (or webserver of choice) document root, so that is not publicly accessible. You will only be able to get to the files using the download script (BTW you must make sure that the download script checks its input to prevent malicious users from downloading other files - like the source of your scripts).
wimvds
allright this is indeed the method i thought about but i too have my concerns about performance and especially when i comes to (streaming?) video, i'm not sure it's even possible... will need todo lots of testing... thanks man! ;-)
Sander Versluys
i see you're questions is very similar to mine, didn't stumble upon it. question updated.
Sander Versluys
If you interested in Performance-oriented way, take a look onto proxy-webserver, such as nginx, which were designed to eliminate connect expenses, and especially at it's X_ACCEL_REDIRECT feature
Col. Shrapnel