Can an included PHP file know where it was included from?

后端 未结 6 1728
遇见更好的自我
遇见更好的自我 2020-12-10 11:31

For example,

This is index.php


Can header.php know it was included by index.php?

6条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-10 11:56

    $_SERVER['PHP_SELF'] should still point to file originally accessed, or you could set a variable before the require, eg:

    $section = 'home';
    require_once('header.php');
    

    ...

    if ($section == 'home') {
        ...
    }
    

提交回复
热议问题