我有个小问题..
我想禁止直接访问我包含的文件。 (例如 header.tpl.php、footer.tpl.php、confic.inc.php、db-connect.inc.php 等)
但首先让我解释一下我想做什么 我希望所有人都允许访问我包含的文件 (index.php),并禁用带有 404 header 的文件以进行直接访问。
现在我找到了一些很酷的 php 片段并对其进行了修改(404 header 和 404 包含)
在我的 index.php 中是这样的代码:
define('MY_APP',true);
在我的模板文件中是这样的代码:
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?
如果您的服务器被重新配置以便 .php 不再执行,它们的源代码将是可见的。
但除此之外,您的方法是一种非常常见的方法。但是 error/404.php
可能包含 header('HTTP/1.1 404 Not Found');
行,因此您不需要为每个文件重复它。 die;
语句也是如此。
在每个库/模板等文件中:
require('../error/include_file.php');
在 include_file.php
中:
if(!defined('MY_APP'))
{
header('HTTP/1.1 404 Not Found');
include('404.php');
die;
}
可能更适合您的设计。不要重复那么多。
关于php - 禁用对包含文件的访问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7715447/