[英]Is it possible to detect when php file is included?
So in file first.php
I will include file original.php
with include_once
. 所以在文件
first.php
我将包含文件original.php
和include_once
。
Problem occurs, when I try to call functions which are inside original.php
and that file is not loading if there is no user session. 当我尝试调用
original.php
函数并且如果没有用户会话则没有加载该文件时,会出现问题。
Basicaly I'm include file original.php
in which are few functions im calling later in first.php
, but those functions are inaccesible due to restrictions I've set in original.php
, but now I want to add rule to original.php
to allow such requests from first.php
or any other PHP file that will include original.php
... Basicaly我包含文件
original.php
,其中很少有函数我稍后在first.php
调用,但是这些函数由于我在original.php
设置的限制而无法访问,但现在我想将规则添加到original.php
允许来自first.php
或任何其他包含original.php
PHP文件的请求...
I wonder what could I do in this case OR could I simply add some if clause to original.php
, like: 我想知道在这种情况下我能做什么或者我可以简单地将一些if子句添加到
original.php
,例如:
if($fileIncluded == 'yes')
{
// do not check if user session exist etc.
}
else
{
// check for user session
}
Thanks for ideas and help in advance! 感谢您的想法和提前帮助!
You can set a constant in the file to be included like so: 您可以在文件中设置一个常量,如下所示:
In original.php
: 在
original.php
:
define('ORIGINAL_INCLUDED', TRUE);
Then in first.php
you check if the constant exists: 然后在
first.php
检查常量是否存在:
if (defined('ORIGINAL_INCLUDED'))
{
// do not check if user session exist etc.
}
else
{
// check for user session
}
Unlike get_included_files() this will work when a script includes a script. 与get_included_files()不同,当脚本包含脚本时,这将起作用。
get_included_files()
will only return the files included in the main script, but not scripts included in child scripts. get_included_files()
只返回主脚本中包含的文件,但不返回子脚本中包含的脚本。
Or in original.php
: 或者在
original.php
:
if ('original.php' == basename(__FILE__))
{
// original.php directly accessed
}
else
{
// original.php included instead
}
PHP provides a get_included_files function that would seem to work in this scenario. PHP提供了一个get_included_files函数,在这种情况下似乎可以正常工作。
if in_array('file.php', get_included_files()) {
/* ... */
}
Well, I am PHP newbie, but I would do 好吧,我是PHP新手,但我会这样做
$add_file = require(file.php);
and then do 然后呢
if($add_file) {echo "file is aded";} else {echo "file is not added";}
You could use the function get_included_files()
or set a variable like this: 您可以使用函数
get_included_files()
或设置如下变量:
// includedfile.php
$variable = true;
//index.php
include 'includedfile.php';
if(isset($variable)) {
// file included
} else {
// not included
}
You could also define a constant, and check if it is defined later. 您还可以定义一个常量,并检查它是否稍后定义。 Use define and defined functions for that.
使用定义和定义的函数。 I think it's better than using normal variables.
我认为这比使用普通变量更好。
Php doc: http://ch2.php.net/manual/en/function.defined.php and http://ch2.php.net/manual/en/function.define.php Php doc: http : //ch2.php.net/manual/en/function.defined.php和http://ch2.php.net/manual/en/function.define.php
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.