What's the best way to only allow a PHP file to be included?

后端 未结 8 2071
梦毁少年i
梦毁少年i 2021-01-04 10:07

I want to make sure people can\'t type the name of a PHP script in the URL and run it. What\'s the best way of doing this?

I could set a variable in the file that wi

相关标签:
8条回答
  • 2021-01-04 10:19

    In a few of the open source applications I've poked around in, including Joomla and PHPBB, they declare a constant in the main includes file, and then verify that constant exists in each of the includes:

    // index.php
    require_once 'includes.php';
    
    // includes.php
    define('IN_MY_PROJECT', true);
    include 'myInc.php';
    
    // myInc.php
    defined('IN_MY_PROJECT') || die("No direct access, plsktnxbai");
    
    0 讨论(0)
  • 2021-01-04 10:21

    From a PHP Nuke module:

    <?php
    if (!eregi("modules.php", $PHP_SELF)) {
       die ("You can't access this file directly...");
    }
    // more code ...
    ?>
    

    Replace modules.php with your file name, and that file cannot be called directly.

    0 讨论(0)
  • 2021-01-04 10:24

    The Zend Framework recommends you keep the files outside the web root, as Unkwntech has suggested. I'd say this is the safest and most fool proof solution.

    0 讨论(0)
  • 2021-01-04 10:27

    You could check the URI and see if that file is being called with `

    $_SERVER['SCRIPT_FILENAME']
    

    or you could move the file outside the public folder, this is a better solution.

    0 讨论(0)
  • 2021-01-04 10:34

    One way I've seen a lot is to create a variable that has to be present in every included file and check first thing in every include:

    if(!isset($in_prog)){
    exit;
    }
    
    0 讨论(0)
  • 2021-01-04 10:35

    I guess the best way is to put files you want to include inside "/include" folder and put access right 700 to the folder

    0 讨论(0)
提交回复
热议问题