TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   get_included_files to find originating script? (http://www.talkphp.com/general/2599-get_included_files-find-originating-script.html)

solistus 04-07-2008 05:13 PM

get_included_files to find originating script?
 
I've asked similar questions a couple times over the past month or so with no conclusive answer.

I need to write a script that knows what the original script being executed is (when it's included). No matter how many layers of nested includes there are, it needs to find the page at the 'top.'

It seems that the array returned by get_included_files always has this as element 0. I haven't been able to find a definite answer as to whether or not this is reliable. It doesn't need to be valid cross-server, so long as it works on PHP 4.4.0 running under apache.

So, would this method work?

Village Idiot 04-07-2008 10:26 PM

I'm not familiar with get_included_files. But from reading the docs, it should work. If its a PHP core function, it should be reliable.

If you for some reason dont want to use that. At the beginning of your page, put this code in
PHP Code:

define("FIRST_PAGE","page.php"); 

FIRST_PAGE is now the originating page. You can place this in as many included files as you want. So long as it is ran before the include statement, it is constant and will not change.

On a sidenote. PHP is a server side language, not a bit of it is ran on the browser. Therefore there is no cross-browser compatibility issues in PHP.

delayedinsanity 04-08-2008 03:39 AM

Quote:

Originally Posted by solistus (Post 13295)
It seems that the array returned by get_included_files always has this as element 0. I haven't been able to find a definite answer as to whether or not this is reliable.

When in doubt, do what I like to do. Make a file called test.php, and code what you need it to do. Then try and make it break. I embedded the call to get_included_files() in a bunch of different ways and it always returned the parent as $included_files_array[0] so it would seem that this method is pretty reliable if not bulletproof.
-m

solistus 04-08-2008 03:50 PM

Village Idiot: I need a method that does not rely on the parent page to do anything special. I support my University's web services, and our content providers (CPs) are our biggest problem security-wise. Quite simply, they do things wrong. We need to determine the parent path from the include without relying on the parent file to do anything special (aside from including our file, whether directly or indirectly through nested includes). Also, I was talking about cross-server compatibility, not cross-browser; so if something would work under mod_php in apache but not with PHP running under Windows or as a CGI, for example, that's fine. A few of the sources I found mentioned minor differences between versions for things like how the directory path is handled, so I thought I'd throw that note in in case it was relevant.

delayedinsanity: I did some basic testing, too. I think I might write a test script that generates random nested include structures to test the code a bit further.

wGEric 04-08-2008 10:16 PM

What about $_SERVER['SCRIPT_FILENAME'] (or one of those variables, I can't remember which exactly)? That will tell you what file was called initially.


All times are GMT. The time now is 03:08 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0