Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
930 views
in Technique[技术] by (71.8m points)

.htaccess to restrict access to folder

I have a php file that inherits (includes) another PHP files. All inherited files are located in one folder called "includes". I want to be able to restrict any direct access to this folder. Only PHP files within the server can communicate with the files in the "includes" folder. How can I use .htaccess to accomplish such goal?

FYI, I used the following code and it did not work

Order deny,allow
deny from all

Any help would be appreciated

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you don't have the option of actually moving the "includes" folder outside of the Document Root area, and using the include_path (i.e. you can't get to it from web), then enter

deny from all

in a .htaccess directory in that directory.

However, alternative is to use a DEFINES directive to only allow access to those include programs by specific authorised files. For example, put

<?php defined('ACCESS_ALLOWED') or die('Restricted Access'); ?>

in the top of the include files, and then put

<?php define('ACCESS_ALLOWED', 1); ?>

in the programs that are allowed to include those files.

This will prevent casual GETs to those include files from running them, and will work for any web server, not just those supporting htaccess files.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...