PHP/Apache Deny folder access to user but not to script

心已入冬 提交于 2019-12-03 19:38:52

问题


So I have this php web app, and one of my folder contains some files that can be downloaded.

I have a download script that modifies the headers, in order to always offer a download link. (instead of showing a picture for example, when you click on a link, a download box pops out)

Right now, if you enter a url like: http://www.mywebsite.com/content/ You get the listing of all the downloadable files, and of course, you can just download them all, without going through the website interface.

Personally, I don't think it's a problem, since I often use downthemall or other downloading tool, and this type of access is a great time saver....

But of course my company does not think so :-p They want people to use the interface in order to view the Ads...

Would they be a way, maybe with a protected .htaccess, to leave the folder access to my download script, but deny access to the users...?

I hope I am making sense and you know what I mean :)

All help/remarks appreciated!


回答1:


You can make a .htaccess file and enter Options -Indexes this will disable listing of the files in the directory.

If you also need the traffic to originate from your site you will need to make a file say... index.php with code that checks $_SERVER['HTTP_REFERER'] to see if the traffic originates from your site.

EDIT

Oh I forgot you can actually fix it all in the .htaccess:

Options -Indexes
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://your-host.com/.*$ [NC]
RewriteRule ^.* /403-page [L,R]

This will do all the work of the script I suggested, so you won't need it anymore.




回答2:


Move the folder out of the webserver's root directory so that apache will not server files from that directory at all. You can still include files from the folder if it is readable by the apache/http user, but your site users won't be able to access it from any url.




回答3:


Yes, this is correct. .access files block access to the users, but has no influence on local serverscripts.




回答4:


Deny from all

in the .htaccess or move the files above document root



来源:https://stackoverflow.com/questions/2667868/php-apache-deny-folder-access-to-user-but-not-to-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!