php - Run code if not running from an include -
page1.php:
include 'page2.php'; echo $info;
page2.php:
$info = "some info"; if(!included){ echo 'why on page?'; }
the goal of if(!included){
determine if page2.php
loaded via include
in page1.php
, or if user requested page directly. there no function in docs returns such value. how can create such function?
you have 2 options:
a) check debug_backtrace
if you're being included
$bt = end(debug_backtrace()); // supposing don't include in functions / other included files etc... if (!empty($bt) && in_array($bt["function"], array("include", "include_once", "require", "require_once"))) { // you're being included! }
b) define constant, variable etc. in page1.php , check in page2.php if exists (defined
or isset
)
Comments
Post a Comment