Solved

Include file

Posted on 2006-11-15
3
241 Views
Last Modified: 2013-11-18
Is there a way to reference which file called the the included file?

--index.php--
<?
include("include.php");
?>

--include.php--
<?
echo $_PARENT //Should print out index.php
?>
0
Comment
Question by:ocsurf
3 Comments
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 65 total points
ID: 17947362
Not natively, no.  You'd have to add some code in index.php to assist with that.

If you're just interested in figuring out which script was requested in the URL, $_SERVER['REQUEST_URI'], or $_SERVER['PHP_SELF'], or various other elements in $_SERVER can tell you that (in various forms, depending on which part of it interests you, basically).  If include.php is included a couple levels deep (so index.php includes somethingelse.php and somethingelse.php includes include.php) then there's no built-in way to figure out those middle levels.
0
 
LVL 16

Accepted Solution

by:
HackneyCab earned 65 total points
ID: 17947612
Good answer, VoteyDisciple. I was just gonna tell him to try $_SERVER['PHP_SELF'], but that may not give him the answer he needs.

ocsurf, if it's critical that you know which file called which include, then perhaps you should use the include file to define a function rather than just run code and echo to output immediately. Then the included function could take an argument from the calling script that identifies which script asked for it.

For example:

--index.php--
<?php
include 'include.php';
get_output('index');
?>

--include.php--
<?php
function get_output($calling_page) {
    echo '<p>I was called from '.$calling_page.'.php this time.</p>';
}

Maybe not the simple answer you're looking for, but possibly the most robust way.
0
 
LVL 1

Author Comment

by:ocsurf
ID: 17947698
thanks
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

948 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now