Jump to content

[SOLVED] How to identify "Who required me"?


duskshine

Recommended Posts

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,

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.

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. ;)

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. :)

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.