Solved

Include file

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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article covers the basics of the Sass, which is a CSS extension language. You will learn about variables, mixins, and nesting.
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…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

707 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

14 Experts available now in Live!

Get 1:1 Help Now