禁止访问包含的文件
我有一个小问题..
我想禁用对我包含的文件的直接访问。 (例如 header.tpl.php、footer.tpl.php、confic.inc.php、db-connect.inc.php 等)
但首先让我解释一下我想做什么 我希望全部允许访问我包含的文件(index.php)并禁用带有 404 标头的文件以进行直接访问。
现在我发现了一些很酷的 php 片段并在我的 index.php 中修改了它(404 标头和 404 包含)
是这样的代码:
define('MY_APP',true);
在我的模板文件中是这样的代码:
if(!defined('MY_APP')) {
header('HTTP/1.1 404 Not Found');
include('./../error/404.php');
die; }
您看到这段代码有任何安全或其他问题吗?
此致 伯恩特
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果您的服务器被重新配置,使得 .php 不再被执行,它们的源代码将是可见的。
但除此之外,您的方法是一种非常常见的方法。但是
error/404.php
可能包含header('HTTP/1.1 404 Not Found');
行,因此您无需为每个文件重复它。die;
语句也是如此。在每个库/模板等文件中:
在
include_file.php
中:可能更适合您的设计。不要重复那么多。
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 theheader('HTTP/1.1 404 Not Found');
line so you don't need to repeat it for each file. Same for thedie;
statement.In each library/template etc. file:
In
include_file.php
:Is maybe better for your design. Don't repeat yourself that much.
为什么不将其放在 public_html 文件夹或任何您用作默认 html 文件夹的上方并包含在 ../../ 中。然后它将可供脚本使用,但公众将得到默认的 404/ 文件未找到。我使用保存密码等的配置文件来执行此操作,这样任何公众都无法访问它们。
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.