I am trying to achieve authenticated file listing in Apache using PHP via Apache’s autoindex module.
The way I imagined it was to have Apache run a PHP script as a header file. I’ve managed to get Apache run PHP correctly for the header file and it detects login cookies fine, too. But it seems that Apache runs the header file as a separate request which means if I try to send a redirection header from PHP it is not run.
My (simplified) Apache config:
DocumentRoot "/path/to/files_root"
Alias /~extra "/path/to/extra-data"
<Directory "/path/to/extra-data">
Options -Indexes -MultiViews +Includes
AllowOverride None
Order allow,deny
Allow from all
</Directory>
IndexOptions FancyIndexing HTMLTable SuppressHTMLPreamble
AddType text/html .php .html .htm
AddOutputFilter INCLUDES .php
AddHandler application/x-httpd-php .php
HeaderName "/~extra/HEADER.php"
My HEADER.php file:
<?php
if ( ! my_validate_cookie_function()) {
header('HTTP/1.1 302 Found');
header('Location: http://login.example.com/');
exit(1);
}
So, the header isn’t sent to the browser. Setting Apache environment viariables doesn’t seem to work, as they are long gone the moment HEADER.php is finished executing.
The cookie itself is encrypted, hence need for PHP to validate it.
Any suggestions how to achieve the desired effect?
Here’s what I finally ended up with. Kudos go to Scott S who hinted me towards the solution.
Apache config:
And (a very simplified version of) my php script:
I actually use Kohana framework with my own libraries to handle authentication checks, display the listing, etc. You will have to take file permissions into account, too. E.g. on *nix systems you will need to check dirs for executable permission, while on Windows read access is enough.