Page 1 of 1

Check file inclusion

Posted: Sun Mar 11, 2007 3:56 pm
by thiscatis
Is there any way to check if a certain php file in included in another php file or accessed directly?

Posted: Sun Mar 11, 2007 4:03 pm
by aaronhall
I'm not sure what you're trying to do, but PHP compiles all includes before parsing them, as if all the code is in the same file -- there is no way to tell which parts of code were included at runtime. You might check include_once() or require_once() just in case I misunderstood you.

Posted: Sun Mar 11, 2007 4:16 pm
by jayshields
If I understand you correctly this (or a variation of it) should work.

Code: Select all

if($_SERVER['PHP_SELF'] != __FILE__) {
  //this file is being accessed indirectly (included)
} else {
  //this file is being access directly (not included)
}

Posted: Sun Mar 11, 2007 4:19 pm
by feyd

Posted: Sun Mar 11, 2007 9:13 pm
by thiscatis
you can use this in the root page which you use to include other files,
this won't give you info to see whether a page is accessed directly for eg (modules/module1.php) or
included in the index.php

Posted: Sun Mar 11, 2007 9:44 pm
by feyd
If you want to check against direct access, use a constant's existence to flag it. It's the easiest to do.