page1.php:
include 'page2.php';
echo $info;
page2.php:
$info = "some info";
if(!included){
echo 'Why are you on this page?';
}
if(!included){
的目标是确定 page2.php
是否通过 page1 中的 include
加载。 php
,或者如果用户直接请求页面。文档中没有返回此类值的函数。如何创建这样的函数?
你基本上有两个选择:
a) 检查 debug_backtrace
是否包括在内
$bt = end(debug_backtrace()); // supposing that you don't include in functions / from other included files etc...
if (!empty($bt) && in_array($bt["function"], array("include", "include_once", "require", "require_once"))) {
// you're being included!
}
b) 在 page1.php 中定义一些常量、变量等,并在 page2.php 中检查是否存在(defined
或 isset
)