PHP include $_SERVER[‘Document_Root’] include_once AJAX

It can be tricky if your include statements don’t always load from the same location in the app, an include(‘that.php’) in the root index file thinks of itself as coming from the index, so anything included THEREIN will too, but then if someday down the line you want to call an evaluation script, for example, and THEN the rest of the application…a bunch might break.

Thus, with EVERY include, it’s best to use one of the available SERVER variables, which is a longer story, but here’s an example:

include($_SERVER[“DOCUMENT_ROOT”] . “/site/lib/”);

The next lesson, logically following, is that this new evaluation script needs to load in the app’s functions…but it thinks the second load (which of course only happens SOME of the time via the evaluations…) is trying to redefine the functions, HENCE:


Prefer it.

Finally, you’ll notice that includes are affected differently when loaded via AJAX.  For example, index.php loads in our new evaluation.php, which then loads 1.php or 2.php and so on, that is, when the app is being loaded exclusively by php in order to be crawled by Googlebot, but if it ISN’T Googlebot visiting it this time, let’s load things via AJAX!  Short and sweet, use the above mentioned $_SERVER[‘Document_Root’] everywhere BUT in your JS, and use full paths if possible INSIDE, as such: http://localhost/includeLesson/pages/page1.php, or similar Server variable to sidestep this mess, and now you can recognize these pitfalls when you encounter them, or remember their solutions next time they cross your path (by which time of course you’ll have forgotten how you solved them last…)

Comments are closed.