duskshine Posted July 31, 2008 Share Posted July 31, 2008 Say I have a.php: ------------ <?php require_once "c.php"; ?> & b.php: ------------ <?php require_once "c.php"; ?> How may I code c.php so that it knows it's required by a.php and not b.php, without changing a.php & b.php? (that is, what should I use to replace the IF_REQUIRED_BY below?) c.php: ------------ <?php IF_REQUIRED_BY("a.php") echo "gotcha!" ?> Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/ Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 Since require literally views the contents of the required file as part of the requiring file, I don't know that this is possible. The only way I know of to do this is to set a variable in the requiring file that is then checked within the required file. Basically, just set up your own check. Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604683 Share on other sites More sharing options...
JD* Posted July 31, 2008 Share Posted July 31, 2008 Grab the url of the current page and do a check...since c.php is included in a.php, the url will contain a.php and then you can have c.php echo that information, or whatever else you need. Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604685 Share on other sites More sharing options...
Darklink Posted July 31, 2008 Share Posted July 31, 2008 You would have to make a definition in A like so: a.php: ----------------- <?php define('SCRIPT_A_LOADED', true); ?> c.php: ----------------- <?php if ( SCRIPT_A_LOADED ) { echo "gotcha!"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604692 Share on other sites More sharing options...
obsidian Posted July 31, 2008 Share Posted July 31, 2008 Grab the url of the current page and do a check...since c.php is included in a.php, the url will contain a.php and then you can have c.php echo that information, or whatever else you need. What about multi-level file includes? a.php includes b.php and b.php includes c.php... If c.php then does a URL check, it returns the faulty data that it was included by a.php instead of b.php. Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604693 Share on other sites More sharing options...
trq Posted July 31, 2008 Share Posted July 31, 2008 Within c.php <?php echo "I have been required by " . basename($_SERVER['SCRIPT_FILENAME']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604695 Share on other sites More sharing options...
trq Posted July 31, 2008 Share Posted July 31, 2008 Grab the url of the current page and do a check...since c.php is included in a.php, the url will contain a.php and then you can have c.php echo that information, or whatever else you need. What about multi-level file includes? a.php includes b.php and b.php includes c.php... If c.php then does a URL check, it returns the faulty data that it was included by a.php instead of b.php. Hmmm, didn't think of that. Ignore my previous reply. Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604697 Share on other sites More sharing options...
JD* Posted July 31, 2008 Share Posted July 31, 2008 What about multi-level file includes? a.php includes b.php and b.php includes c.php... If c.php then does a URL check, it returns the faulty data that it was included by a.php instead of b.php. In that case then yes, you would need to have a variable scope, or instead you'd have to create a function for including c.php, where you can report the file name of the current file calling the function. Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-604698 Share on other sites More sharing options...
duskshine Posted August 1, 2008 Author Share Posted August 1, 2008 thank you very very much guys! * problem solved * Quote Link to comment https://forums.phpfreaks.com/topic/117569-solved-how-to-identify-quotwho-required-mequot/#findComment-605214 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.