0

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!

Community
  • 1
  • 1
Monty
  • 1,304
  • 4
  • 19
  • 37

2 Answers2

2

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")
      ...
?>
Jules
  • 7,148
  • 6
  • 26
  • 50
1

There's a set of magic constants available to get that kind of information, but I don't think they cover what you want. Are you after something like:

mainfile.php:

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

included.php:

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

which would output

this page was included from mainfile.php

note: PARENTFILE does not exist and does not work. just making it up to figure out if this is what the OP means.

Marc B
  • 356,200
  • 43
  • 426
  • 500