I’m learning PHP in order to simplify one of the websites I maintain.

It turns out that SSI (server-side includes) and PHP are incompatible – you can’t use both at once, so I have to convert all my SSI includes into PHP includes.


I’ve been using the “virtual” SSI mode, as in:

<!–#include virtual=”/INCLUDE/_HEADER.html” –>

This is nice because it’s relative to the site root (instead of relative to the calling document location), so you can cut-and-paste include directives without worrying about what folder the calling document is in.

Unfortunately, PHP only does document-relative inclusion, using include() or require().  So you have to do something like:

<?php include(“/home/dave/public_html/nerdfever/INCLUDE/_HEADER.html”); ?>

I guess that would be OK except that I run test sites on another domain, and keep the files in another folder (think “testsite” instead of “nerdfever” in the path above).  That means all my include() calls need to change when switching from the test site to the live site – which defeats the purpose of testing.


I found a trick that solves the problem.  There’s a global variable in PHP called $DOCUMENT_ROOT – so you just do this:

<?php include(“$DOCUMENT_ROOT/INCLUDE/_HEADER.html”); ?>

Strangely, I couldn’t find anyone else posting this solution on the web, so I thought I’d post it here.

There are other solutions, but they’re either more complicated or they require support of the PHP virtual() function, which isn’t allowed on most shared hosts.

Have fun.