所以在文件 first.php
中我将包含文件 original.php
和 include_once
。
当我尝试调用 original.php
中的函数并且如果没有用户 session 时不会加载该文件时,就会出现问题。
基本上,我包含文件 original.php
,其中有几个函数我稍后会在 first.php
中调用,但由于我的限制,这些函数无法访问在 original.php
中设置,但现在我想向 original.php
添加规则以允许来自 first.php
或任何其他 PHP 的此类请求将包含 original.php
...
我想知道在这种情况下我能做什么,或者我可以简单地在 original.php
中添加一些 if 子句,例如:
if($fileIncluded == 'yes')
{
// do not check if user session exist etc.
}
else
{
// check for user session
}
提前感谢您的想法和帮助!
最佳答案
您可以在文件中设置一个常量,如下所示:
在original.php
中:
define('ORIGINAL_INCLUDED', TRUE);
然后在 first.php
中检查常量是否存在:
if (defined('ORIGINAL_INCLUDED'))
{
// do not check if user session exist etc.
}
else
{
// check for user session
}
不同于 get_included_files()当脚本包含脚本时,这将起作用。 get_included_files()
只会返回主脚本中包含的文件,不返回子脚本中包含的脚本。
或者在original.php
中:
if ('original.php' == basename(__FILE__))
{
// original.php directly accessed
}
else
{
// original.php included instead
}
关于php - 是否可以检测何时包含 php 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14297821/