$_SERVER['DOCUMENT_ROOT'] Doesn't Include My Web Site Root Directory

Due to the clustered system of Apache servers used to handle PHP files, if you use $_SERVER['DOCUMENT_ROOT'] in your code you will need to specify it in one of two ways.

First, you can specify it in the script using it. This is an easy option if you only have one script you use this variable in. If you use this variable in multiple scripts, use the second option. To specify $_SERVER['DOCUMENT_ROOT'] in a script, use the following before you call it:

$_SERVER['DOCUMENT_ROOT'] = '/mnt/Target01/123456/www.mydomain.com/web/content/';

where /mnt/Target01/123456/www.mydomain.com/web/content/ is replaced by your web site's file system path which can be found in the Features section of the hosting control panel when viewing your domain.

 

Or, you can specify it in a .htaccess file using the auto_prepend_file flag. To do this, create a PHP file to store the $_SERVER['DOCUMENT_ROOT'] setting (and any other settings you need available to all scripts). For security, you can save this outside your web site root (the 'web/content' directory). For example, we can put the following in a file named config.inc.php and save it in the directory above our web site root ('web' directory):

<?
$_SERVER['DOCUMENT_ROOT'] = '/mnt/Target01/123456/www.mydomain.com/web/content/';
?>

Replace /mnt/Target01/123456/www.mydomain.com/web/content/ with your web site's file system path which can be found in the Features section of the hosting control panel when viewing your domain. Now, add the following to a .htaccess file in the root of your site:

php_value auto_prepend_file "/mnt/Target01/123456/www.mydomain.com/web/config.inc.php"

Replace /mnt/Target01/123456/www.mydomain.com/web with the value from above.

Warning: These variables will show up in phpinfo() so if your going to add new server variables that contain sensitive information like passwords, makes sure you don't have any phpinfo() open to the public.

  • 86 Users Found This Useful
Was this answer helpful?

Related Articles

Client IP from $_SERVER['REMOTE_ADDR'] is Always the Same

If your scripts use $_SERVER['REMOTE_ADDR'] to get the client's IP address, they will always show...