0

可能重复:
获取包含文件中当前 PHP 脚本的名称
包含的 PHP 文件可以知道它是从哪里包含的吗?

这个标题有意义吗?:)

您能从包含的页面中确定PHP您放入的<?php include 'SomeFile.php'; ?>页面吗?

在要“ INCLUDED”的页面中是否有基于抓取它的目的地的 if 语句?

我这样说对吗?

谢谢大家!

解决方案:

PAGEMAIN1.php

<?php
$MAIN2 ='';
$MAIN1 = 'MAIN1';
include 'PAGE1.php';
include 'PAGE2.php';
include 'PAGE3.php';
include 'PAGE4.php';
?>

PAGEMAIN2.php

<?php
$MAIN1 ='';
$MAIN2 = 'MAIN2';
include 'PAGE1.php';
include 'PAGE2.php';
include 'PAGE3.php';
include 'PAGE4.php';
?>

PAGE1.php

<?php
$PAGE1 = 'PAGE1';
if($MAIN1 == 'MAIN1'){
    echo '(This is PAGE 1 being "INCLUDED" in MAIN 1)<br>';
    }
if($MAIN2 == 'MAIN2'){
    echo '(This is PAGE 1 being "INCLUDED" in MAIN 2)<br>';
    }
?>

PAGE2.php

<?php
$PAGE2 = 'PAGE2';
if($MAIN1 == 'MAIN1'){
    echo '(This is PAGE 2 being "INCLUDED" in MAIN 1)<br>';
    }
if($MAIN2 == 'MAIN2'){
    echo '(This is PAGE 2 being "INCLUDED" in MAIN 2)<br>';
    }
?>

PAGE3.php

<?php
$PAGE3 = 'PAGE3';
if($MAIN1 == 'MAIN1'){
    echo '(This is PAGE 3 being "INCLUDED" in MAIN 1)<br>';
    }
if($MAIN2 == 'MAIN2'){
    echo '(This is PAGE 3 being "INCLUDED" in MAIN 2)<br>';
    }
?>

PAGE4.php

<?php
$PAGE4 = 'PAGE4';
if($MAIN1 == 'MAIN1'){
    echo '(This is PAGE 4 being "INCLUDED" in MAIN 1)<br>';
    }
if($MAIN2 == 'MAIN2'){
    echo '(This is PAGE 4 being "INCLUDED" in MAIN 2)<br>';
    }
?>

PAGEMAIN1.php 的输出

(This is PAGE 1 being "INCLUDED" in MAIN 1)
(This is PAGE 2 being "INCLUDED" in MAIN 1)
(This is PAGE 3 being "INCLUDED" in MAIN 1)
(This is PAGE 4 being "INCLUDED" in MAIN 1)

PAGEMAIN2.php 的输出

(This is PAGE 1 being "INCLUDED" in MAIN 2)
(This is PAGE 2 being "INCLUDED" in MAIN 2)
(This is PAGE 3 being "INCLUDED" in MAIN 2)
(This is PAGE 4 being "INCLUDED" in MAIN 2)

以防万一你想知道。;) 谢谢!

4

2 回答 2

2

您可以做的是创建一个在包含子页面的所有页面中使用的全局变量。然后,您可以在子页面中检查您所在的页面。那有意义吗?;-)

第 1 页:

<?php
   $THISPAGE = "page1";
   include("subpage.php");
?>

第2页:

<?php
   $THISPAGE = "page2";
   include("subpage.php");
?>

子页面:

<?php
   if ($THISPAGE == "page1")
      ...
?>
于 2012-04-05T20:06:07.783 回答
1

有一组魔术常数可用于获取此类信息,但我认为它们无法涵盖您想要的内容。你是否在追求类似的东西:

主文件.php:

<?php include('included.php'); ?>

包含的.php:

this page was included from <?php echo __PARENTFILE __ ?>

这将输出

this page was included from mainfile.php

注意:PARENTFILE不存在且不起作用。只是为了弄清楚这是否是OP的意思。

于 2012-04-05T20:05:15.660 回答