Php:如何识别"include "一页的


php: how to identify the "includes" of a page

我在pageA.php中使用以下代码

if (DOWNLOAD_ENABLED == 'true') include 'pageB.php';

是有可能知道在pageB.php, pageA.php包括它?我需要根据包含它的页面在pageB.php中执行某些操作。

在pageA中声明的任何变量都可以在pageB中访问,这不是很好的做法,你应该使用面向对象或至少是函数来实现这一点,但如果你绝对有必要,你可以这样做:

// on pageA.php
if (DOWNLOAD_ENABLED == 'true') {
  $includedFrom = "pageA";
  include 'pageB.php';
}
// on pageB.php
switch($includedFrom) {
  case "pageA":
    // do stuff
    break;
}

最简单的方法是将其保存在var中。

$my_page = 'pageA.php';
if (DOWNLOAD_ENABLED == 'true') include 'pageB.php';

pageB.php中可以正常访问$my_page

可以这样做:

pageA.php

define('AUTHORISED_TO_INCLUDE', 1);
if (DOWNLOAD_ENABLED == 'true') include 'pageB.php';

pageB.php

if(!defined('AUTHORISED_TO_INCLUDE')) // Do something, e.g. die();