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

その種の情報を取得するために利用できる一連の魔法の定数がありますが、それらがあなたが望むものをカバーしているとは思いません。あなたは次のようなものを求めていますか?

mainfile.php:

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

include.php:

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

出力します

this page was included from mainfile.php

注:PARENTFILEは存在せず、機能しません。これがOPの意味であるかどうかを理解するためにそれを作り上げるだけです。

于 2012-04-05T20:05:15.660 に答える