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?
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…