Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Include file

Posted on 2006-11-15
3
243 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo‚Ķ
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

791 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