如何检测文件是否被包含或直接运行 [英] How to detect if a file is being included or directly ran

查看:165
本文介绍了如何检测文件是否被包含或直接运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个php文件,我包含在我的PHP脚本中,但我不希望人们能够直接运行该文件(不包括在内)。
如何防止这种情况发生?

I have a php file that I include in my php script, but I don't want people to be able to directly run the file(without being included). How can I prevent that from happening?

推荐答案

使所包含的脚本无法通过HTTP访问即可。例如。通过保护子文件夹或将它们移动到文档根目录之上。

Make the included scripts not accessible via HTTP at all. E.g. by protecting the subfolder or moving them above the document root.

如果你不能这样做, define()某事如主脚本中的 IS_INCLUDED 退出; 如果此常量不是 defined()在您附带的脚本中。

If you cannot do that, define() something like IS_INCLUDED in your main script and exit; if this constant is not defined() in your included script.

这篇关于如何检测文件是否被包含或直接运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆