编程语言
首页 > 编程语言> > php-禁止访问包含的文件

php-禁止访问包含的文件

作者:互联网

我有一个小问题.

我想禁用对包含文件的直接访问.
(示例header.tpl.php,footer.tpl.php,confic.inc.php,db-connect.inc.php等.)

但首先让我解释一下我想做什么
我想所有人都允许访问我包含的文件(index.php),并禁用带有404标头的文件以进行直接访问.

现在我发现了一些很酷的php代码片段并对其进行了修改(404标头和404包含)

在我的index.php中是以下代码:

define('MY_APP',true);

在我的templatefiles中是以下代码:

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

您看到此代码有任何安全性或其他问题吗?

最好的祝福
bernte

解决方法:

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

如果您的服务器被重新配置,以致.php不再执行,则它们的源代码将可见.

但是,除此之外,您的方法是执行此操作的一种非常常见的方法.但是error / 404.php可能包含标题(“ HTTP / 1.1 404 Not Found”);行,因此您无需为每个文件重复此操作.死也一样;声明.

在每个库/模板等文件中:

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

在include_file.php中:

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

也许对您的设计更好.不要重复太多.

标签:php,include,http-status-code-404
来源: https://codeday.me/bug/20191011/1895013.html