Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.htaccess to restrict access to folder

Tags:

.htaccess

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

like image 902
Eyad Fallatah Avatar asked Jan 06 '11 00:01

Eyad Fallatah


People also ask

How do I restrict access to a folder in PHP?

By default, PHP does not restrict which files and directories your PHP scripts can access. To restrict the directories that can be accessed, you can use PHP's open_basedir setting.


2 Answers

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.

like image 67
Reuben Avatar answered Sep 20 '22 20:09

Reuben


in your includes/.htaccess :

Order deny,allow
deny from all
like image 21
jujule Avatar answered Sep 20 '22 20:09

jujule