Preventing direct access to php files

前端 未结 3 2034
时光取名叫无心
时光取名叫无心 2020-12-07 05:39

Here is an example of my file structure.

/index.php
/inc
/inc/file1.php
/inc/file2.php
/inc/file3.php
/search.php

index.php includes() file

相关标签:
3条回答
  • 2020-12-07 06:06

    In index.php you can define a constant like this:

    define('RESTRICTED',1);
    

    Then in your other pages you put this after <?php

    if(!defined('RESTRICTED'))exit('No direct script access allowed!');
    

    When the other pages are accessed directly the code above sees that RESTRICTED has not been set, hence it will exit.

    0 讨论(0)
  • 2020-12-07 06:13

    Get them outside the document root:

    /documentroot/index.php
    /inc/file1.php
    /inc/file2.php
    /inc/file3.php
    

    So, there is no direct access to them whatsoever.

    0 讨论(0)
  • 2020-12-07 06:20

    make a .htaccess file and put it in the inc folder

    <Files .htaccess>
    order allow,deny
    deny from all
    </Files>
    
    <Files *.php>
    order allow,deny
    deny from all
    </Files>
    

    or put this at top of file you want to denie

    if(strrchr($_SERVER['PHP_SELF'],'/')=='/file_name.php'){die;} // not good for ajax include
    
    0 讨论(0)
提交回复
热议问题