tags:

views:

21

answers:

3

If a site has php session's in place to enforce authentication/authorization to pages on the site which are implemented in php, how does the same logic enforce access to certain files.

Lets say a repository of files in a directory. So /var/www/html/ is protected via authentication however, this PHP authentication logic won't prohibit a user from simply going to http://site.com/someDirectory/fileIShouldNotAccess.txt and pulling that file.

How do you couple the php session and authentication with apache to enforce this type of behavior?

A: 

A couple answers:

1) make your php sessions use HTTP authentication. Then you can use a .htaccess file to control file access in directories

2) Use mod_rewrite to redirect all requests to a "front controller". Let the front controller manage whether access is allowed, denied, or forwarded to a different controller module for further processing.

Zak
Can you explain 1 a bit more, and it seems to me that 2 would take significantly more work compared to 1, am I correct ?
Chris
Basically, you make sure you are running php as an apache module. This link then fills you in on the exact headers to initiate http based auth:http://php.net/manual/en/features.http-auth.php
Zak
A: 

You can try HTTP Authentication with PHP. This article might help.

rubayeet
How would this work in regards to potentially using external authentication such as LDAP, or Shibboleth?
Chris
+1  A: 

Since PHP won't be invoked when the user requests a non-PHP file, you can't have Apache enforce PHP's access protection. You can make a very coarse and easy-to-fake check in Apache to make sure that a session ID cookie is present, but that's highly insecure. It just checks if the cookie's there, not that it represents a valid session or that the user's actually been granted access.

This other answer might help. http://stackoverflow.com/questions/2187200/using-php-apache-to-restrict-access-to-static-files-html-css-img-etc. Basically, you serve up all the protected content via a PHP script, instead of providing direct access.

Marc B