December 27, 2005 23:02 | WebTech


Remember I asked about "when was the last time you wrote out an entire webpage" a few weeks back?

Good lord that was still in this same month! So much has happened since then... I remember discussing this at Laika with Karl even! A lifetime ago...

Anyways, sort of lost in that entry was the point that I haven't built a whole website by hand in ages. For the last few years it's due to all these fancy CMS we now have, but even before that I had my own little PHP-based package which allowed me to build entire websites, navigation and infrastructure and plumbing and everything, in minutes. Set it up, put all the content in, move stuff around until "the client" is happy with the architecture.. then skin it. (Its predecessor was hacked together with xSSI variables!)

I have to put up a whole site pronto for something very important. I just tried setting up my little wonder on two different servers (one being "the client's") and it unceremoniously failed. These high-falutin' fancy hosting packages break when you ask for $_SERVER['DOCUMENT_ROOT'], which means I seem to be shit outta luck.

I am faced with the prospect of a ... gulp... static website! Argh!! Over my dead body.

Adriaan, we gotta fix yer goddamn server. ;p

Just quickly, here are four totally different paths on the same system all supposedly poitning to the same "place". The first three are available to PHP and th efourth is what I see in my SFTP client:

  DOCUMENT_ROOT = /home/www/htdocs
SCRIPT_FILENAME = /home/www/vhosts/
       __FILE__ = /home/www/vhosts/
           SFTP = /home/name/web/www/
Obviously the next step is to actually try each one, but eh, I'm going to bed now. ;)

Perhaps it's because I don't know better, but I never trusted $_SERVER["DOCUMENT_ROOT"]. I think it may not work in a virtual hosting environment, and it doesn't exist in IIS either.

What I usually do is switch on $_SERVER["SERVER_NAME"] and set my own root (etc.) global variables depending on the server.

For example:

switch ($_SERVER["SERVER_NAME"]) {
  case "localhost":
    // My local environment
    $GLOBALS["C_sToRoot"] = "/thesite/";
    $GLOBALS["C_sToRootServer"] = "e:/web/";
  case "":
  case "":
    // The live site
    $GLOBALS["C_sToRoot"] = "/";
    $GLOBALS["C_sToRootServer"] = "/home/thesite/public_html/";

Try using .. "$_ENV" instead ? Have you tried the phpinfo() function ?

Hehehe, thanks guys but I was just being dramatic. I know what I need to find in order for it to work for me, just didn't have the time at the moment and felt like ranting. ;)

Patrick, thanks for that code, very clever! It would fit nicely in my bootstrap file but it doesn't help me with the include I need to make on every single page of the site (I use the filesystem as the content database. ;)


Man, you're making an elephant out of a mosquito. I already told you yesterday to take the path from either SCRIPT_FILENAME or __FILE__ which are actually both the same and give you the server path. The path you see in ssh is the "physical" path, which you shouldn't rely on for your includes.

Never rely on DOCUMENT_ROOT on a server that hosts more than one domain.

cryin wolf eh !! ;-)

naaaah just a big baby ;)

So it seems I am out of luck this time. None of the other easily available paths will do the trick. The key here is that I have a site consisting of an actual file-system. Every file, in its directory and subdirectory contains an initial include(); directive which needs to go fetch the bootstrap file. The beauty of using DOCUMENT_ROOT is that i don't have to edit the include path on all those files.

And Ado, saying "never rely on DOCUmENT_ROOT on a server that hosts more than one domain is bull. I have been using this code for over 6 years, on hundreds of different hosts, all of them shared servers, and I have never had this problem. Basically, I don't know how yer setup is done, but it is wrong somewhere since DOCUMENT_ROOT should point to the this domain's web root. Always does. ;)