$DOCUMENT_ROOT variable problem

I am trying to use the $DOCUMENT_ROOT variable in my php file and when I view it in the browser, it says “Variable undefined” Why doesnt it work? Any help would be greatly appreciated.

Thanks in advance,

You may have to use $_SERVER[‘DOCUMENT_ROOT’]; if register_globals is off in your php.ini file.

Thanks for the info. So do i just have to turn the register globals on to use the variable? so that i dont have to use the $_SERVER[‘DOCUMENT_ROOT’]; Also, is any of them better to use, like is one of them more stable or safer or something, or does it matter?


For security purposes, it is best to leave register_globals turned off.

PHP versions > 4.1 support the $_SERVER super-global associative array. Older versions need to use $HTTP_SERVER_VARS associative array.

If you’re just coding for one server, I recommend using the $_SERVER array. If your PHP version doesn’t support, you should upgrade.

If the code is intended to be portable, you can use a little trick I came up with…

function server_vars($var){
  list($ver_major, $ver_minor) = explode('.', phpversion());
  $phpversion = (int) "$ver_major.$ver_minor";
  if(4.1 < $phpversion){
    return $_SERVER[$var];
    global $HTTP_SERVER_VARS;
    return $HTTP_SERVER_VARS[$var];
//show document root
echo server_vars('DOCUMENT_ROOT');