disable access to included files

纵然是瞬间 提交于 2020-01-11 10:15:23

问题


i have a litte question..

i want to disable the direct access to my included files. (example header.tpl.php, footer.tpl.php, confic.inc.php, db-connect.inc.php ect.)

but first let me explain what i wanna do i want to all allow the access for my included files (index.php) and disable the files with a 404 header for direct access.

now i found some cool php snippet and modified it (404 header and 404 include)

in my index.php is this code:

define('MY_APP',true);

in my templatefiles is this code:

if(!defined('MY_APP')) {
header('HTTP/1.1 404 Not Found');
include('./../error/404.php');  
die; }

do you see any security or other problems with this code?

best regards bernte


回答1:


do you see any security or other problems with this code?

In case your server is re-configured so that the .php don't get executed any longer, their source-code will be viewable.

But next to that your approach is a quite common way to do that. However error/404.php could contain the header('HTTP/1.1 404 Not Found'); line so you don't need to repeat it for each file. Same for the die; statement.

In each library/template etc. file:

require('../error/include_file.php');

In include_file.php:

if(!defined('MY_APP'))
{
    header('HTTP/1.1 404 Not Found');
    include('404.php');  
    die; 
}

Is maybe better for your design. Don't repeat yourself that much.




回答2:


Why not just tuck it above the public_html folder or whatever you use as the default html folder and include with ../../. Then it would be available to scripts but the public would get a default 404/ file not found. I do this with config files that hold passwords and such so no one public can access them.




回答3:


if (basename($_SERVER['SCRIPT_FILENAME']) == basename(__FILE__))
{
    //header("Location: index.php");
    exit("NOT ALLOWED");
}


来源:https://stackoverflow.com/questions/7715447/disable-access-to-included-files

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!