dongre6404 2012-04-05 20:01
浏览 74
已采纳

使用PHP include,您可以在包含的页面中定义包含页面吗? [重复]

Possible Duplicate:
get name of current PHP script in include file
Can an included PHP file know where it was included from?

Does that headline make sense? :)

Can you determine the PHP page that you put the <?php include 'SomeFile.php'; ?> into, FROM THE INCLUDED PAGE??

In the page to be 'INCLUDED' have an if statement based on the destination that grabs it?

Am I even saying this right?

Thanks All!

THE SOLUTION:

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>';
    }
?>

OUTPUT FROM 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)

OUTPUT FROM 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)

Just in case you wanted to know. ;) Thanks!

  • 写回答

2条回答 默认 最新

  • doqau82086 2012-04-05 20:06
    关注

    What you can do is make a global variable that you use in all the pages that includes the subpage. In the subpage you can then check which page you are on. Does that make sense? ;-)

    page1:

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

    page2:

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

    subpage:

    <?php
       if ($THISPAGE == "page1")
          ...
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗