Prevent direct access to a php include file

后端 未结 30 970
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:32

I have a php file which I will be using as exclusively as an include. Therefore I would like to throw an error instead of executing it when it\'s accessed directly by typing

相关标签:
30条回答
  • 2020-11-22 07:02

    Besides the .htaccess way, I have seen a useful pattern in various frameworks, for example in ruby on rails. They have a separate pub/ directory in the application root directory and the library directories are living in directories at the same level as pub/. Something like this (not ideal, but you get the idea):

    app/
     |
     +--pub/
     |
     +--lib/
     |
     +--conf/
     |
     +--models/
     |
     +--views/
     |
     +--controllers/
    

    You set up your web server to use pub/ as document root. This offers better protection to your scripts: while they can reach out from the document root to load necessary components it is impossible to access the components from the internet. Another benefit besides security is that everything is in one place.

    This setup is better than just creating checks in every single included file because "access not permitted" message is a clue to attackers, and it is better than .htaccess configuration because it is not white-list based: if you screw up the file extensions it will not be visible in the lib/, conf/ etc. directories.

    0 讨论(0)
  • 2020-11-22 07:02

    Do something like:

    <?php
    if ($_SERVER['SCRIPT_FILENAME'] == '<path to php include file>') {
        header('HTTP/1.0 403 Forbidden');
        exit('Forbidden');
    }
    ?>
    
    0 讨论(0)
  • 2020-11-22 07:03

    An alternative (or complement) to Chuck's solution would be to deny access to files matching a specific pattern by putting something like this in your .htaccess file

    <FilesMatch "\.(inc)$">
        Order deny,allow
        Deny from all
    </FilesMatch>
    
    0 讨论(0)
  • 2020-11-22 07:07

    What Joomla! does is defining a Constant in a root file and checking if the same is defined in the included files.

    defined('_JEXEC') or die('Restricted access');
    

    or else

    one can keep all files outside the reach of an http request by placing them outside the webroot directory as most frameworks like CodeIgniter recommend.

    or even by placing an .htaccess file within the include folder and writing rules, you can prevent direct access.

    0 讨论(0)
  • 2020-11-22 07:09

    The easiest way for the generic "PHP app running on an Apache server that you may or may not fully control" situation is to put your includes in a directory and deny access to that directory in your .htaccess file. To save people the trouble of Googling, if you're using Apache, put this in a file called ".htaccess" in the directory you don't want to be accessible:

    Deny from all
    

    If you actually have full control of the server (more common these days even for little apps than when I first wrote this answer), the best approach is to stick the files you want to protect outside of the directory that your web server is serving from. So if your app is in /srv/YourApp/, set the server to serve files from /srv/YourApp/app/ and put the includes in /srv/YourApp/includes, so there literally isn't any URL that can access them.

    0 讨论(0)
  • 2020-11-22 07:10

    Add this to the page that you want to only be included

    <?php
    if(!defined('MyConst')) {
       die('Direct access not permitted');
    }
    ?>
    

    then on the pages that include it add

    <?php
    define('MyConst', TRUE);
    ?>
    
    0 讨论(0)
提交回复
热议问题