?
Solved

Include file

Posted on 2006-11-15
3
Medium Priority
?
247 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
[X]
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
3 Comments
 
LVL 19

Assisted Solution

by:VoteyDisciple
VoteyDisciple earned 260 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 260 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

New benefit for Premium Members - Upgrade now!

Ready to get started with anonymous questions today? It's easy! 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 several years ago, it seemed like now was a good time to update it for object-oriented PHP.  This article does that, replacing as much as possible the pr…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

762 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