tags:

views:

77

answers:

4

Hello,

Is it possible to "deny from all" apache htaccess style using php.

I can't use htaccess because im using different webserver, so i wan't to use php to workaround it.

So let say user are trying to access folder name 'david', all content and subdirectory are denied from viewing.

+1  A: 

Use chmod to change the permissions on that directory. Note that the user running PHP needs to own it in that case.

Joey
And so what? Why don't just wipe this catalog from the disk instead?
Col. Shrapnel
Now you got me confused. What catalog? Why wipe anything?
Joey
Catalog of which mckenzie were asking. Setting it's permissions to deny webserver from read will be equal to wiping it's contents from disk.
Col. Shrapnel
That would only work if php is running as a different user than apache. (and that would also mean it's a cgi or a fastcgi of some sort, not an apache module) And accessing the file with a different set of permissions over than the one used by apache (let's day apache uses the "other" set and php the "group" set) it's a weird server configuration and it has some drawbacks from the point of view of the sysadmin.
ZJR
Could make sense on windows server ACLs, though.
ZJR
ZJR: FastCGI isn't exactly uncommon, though. Though you are right, it begs the question of how exactly to use those weird permissions UNIX systems give you.
Joey
+3  A: 

No
PHP cannot be used to protect folders.
Because it is not PHP who serves requests, but a web server

You can move this catalog above Document Root to prevent web access to it.

But premissions will help you nothing

Col. Shrapnel
+1  A: 

If you just want to prevent indexing the folder, you can create an index.php file that does a simple redirection. Note: Requests that have a valid filename will still be let through.

<?php
   header("Location: /"); // redirect user to root directory
gnarf
this would be equal to `Options -Indexes`, not `deny from all`. though it's still can be what mckenzie *meant*.
Col. Shrapnel
A: 

Without cooperation from the webserver the only way to protect your files is

  • to encrypt them, in an archive, maybe, of which your script would know the password and tell no one - that will end up wasting cpu as the server will be decrypting it all the time, or

  • to use an incredibly deranged file naming scheme, a file naming scheme you won't ever describe to anyone, and that only your php script can sort trough.

Still data could be downloaded, bandwidth go to waste and encrypted files decrypted.

It all depends on how much that data matters. And how much your time costs, as these convoluted layers of somewhat penetrable obfuscation will likely eat huge chunks of developer time.

Now, as I said... that would be without cooperation from the webserver... but what if the webserver is cooperating and doesn't know?

I've seen some apache webservers, (can anyone confirm it's in the standard distribution?) for instance, come preloaded with a rule denying access to files starting with .ht, not only .htaccess but everything similar: .htproxy, .htcache, .htwhatever_comes_to_mind, .htyourmama...
Chances are your server could be one of those.

If that's the case... rename your hidden files .hthidden-<filename1>,.hthidden-<filename2>... and you'll get access to them only through php file functions, like readfile()

ZJR
I confirm `.ht*` exclusion is in the standard distribution and is designed this way to exclude `.htaccess` **and** `.htpasswd` files
ZJR