PHP Security - Block Access to Include Files Using .htaccess

When I build websites for clients and myself, I use numerous include files to make my website easy to maintain. These include files may:

  • be composed of pure HTML; no server-side programming involved
  • be PHP class files; used throughout the website
  • composed of both HTML and PHP
  • PHP code to produce a specific action; many times, AJAX scripts

Obviously, if a person were to get lucky and guess the path and file name of my include scripts, problems could result, especially if an AJAX script is not secured (but I wouldn't do that — nor would you, right?). For example, take the following poorly coded bit of PHP that would get run when an AJAX call was made:

//inside file:   includes/ajax/delete_id.inc
$query = 'DELETE FROM my_table WHERE id = '.$_GET['id'];
mysql_query($query);

Imagine if the user changed the 'id' in the querystring to "' or 1" — all data would be lost! (There's really no excuse for having an unprotected script, but this is a simple example)

Even if my scripts are secure (meaning I use proper validation to make sure they've been called correctly), a user/hacker has no business calling an include file. Using .htaccess, we can prevent any attempt by a user to reach an include file:

<FILES ~ "\.inc$">
Order allow,deny
Deny from all
</FILES>

The above code tells the server to disallow any requests by the user for any file ending in ".inc". You can easily modify the above .htaccess for your own naming convention and folder structure.

Do you employ this type of system? Do you have any ideas for improvement?

0
Average: 3.5 (2 votes)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Comments

Cal Evans replied on Wed, 2008/01/30 - 9:01am

Hi David,

Great tip!

Another way to prevent prying eyes from getting to your classes, password files, etc is to locate all non-essential files outside of your web root. Apache can't serve what Apache can't see but by adjusting your include_path you can still access all of your files from within PHP. 

=C= 

http://devzone.zend.com
http://blog.calevans.com

David Walsh replied on Wed, 2008/01/30 - 9:20am

Good call Cal. I suppose this would be used more when placing a website on shared hosting where you aren't allowed to post outside of root.

Thank you for your tip as well!

David Walsh
Web Developer & Zone Leader
http://davidwalsh.name/

rocky1138 replied on Wed, 2008/01/30 - 9:43am

Also, if you set Apache to read the extension .inc as a PHP file it shouldn't matter if someone can view it from the outside world (unless they have a connection to your server on anything other than Apache's port [typically port 80]) No? All they would get is the compiled php file. Or am I wrong?

David Walsh replied on Wed, 2008/01/30 - 10:42am

True, Rocky, but this adds a level of security for peace of mind (if nothing else) because you can't always count on shared hosts.   You could just as easily change the .htaccess code to ".php" if you use PHP includes.

David Walsh
Web Developer & Zone Leader
http://davidwalsh.name/

Jon Gilkison replied on Wed, 2008/01/30 - 9:00pm

Two things:

a. Put your includes outside of your public directory.

b. .htaccess files are a major performance bottleneck and should be avoided at all costs. 

David Walsh replied on Thu, 2008/01/31 - 10:01am

Thank you for your opinion 406.

David Walsh
Web Developer & Zone Leader
http://davidwalsh.name/

Philippe Lhoste replied on Thu, 2008/02/07 - 8:09am in response to: rocky1138

Well, the PHP file will be executed anyway, which can be dangerous, particularly with the given example!

I saw a trick, which is to set a define in the main module, and to test it is defined in the include file: if not set, it means the include file was called directly, so the script should die immediately.

fourcs replied on Sun, 2008/09/14 - 9:55am

Good post. But I've got a situation where I have login and actions after login. My action files are exposed and can fill my database. I've used your approach to hiding include files but that doesn't work where I call include add  files.  Perhaps the best approach is like someone said: keep them out of publc domain.

Shown replied on Mon, 2009/10/05 - 2:30am

The PCI Security Standards Council will enhance the PCI DSS as needed to ensure that the standard includes any new or modified requirements necessary to mitigate emerging payment security risks, while continuing to foster wide-scale adoption.

John
pci security

http://devzone.zend.com

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.