IIS Servers Do Not Always Set Document Root

IIS servers doesn’t always set the PHP standard variable, $_SERVER[‘DOCUMENT_ROOT’]

How do you set that in one place, your configuration file that you include in all your php programs, so the rest of your code has the document root set like on Apache servers?

Output $_SERVER to see what IS given that you might be able to use:

echo "
_SERVER:
"; print_r($_SERVER); echo "

_ENV:
"; print_r($_ENV); echo "

";

In this example, the SCRIPT_FILENAME and SCRIPT_NAME are set.

Modify the code below to use what is given to get DOCUMENT_ROOT:

if (!isset($_SERVER['DOCUMENT_ROOT']) || $_SERVER['DOCUMENT_ROOT'] === '') {
  $_SERVER['DOCUMENT_ROOT'] = substr($_SERVER['SCRIPT_FILENAME'], 0, -strlen($_SERVER['SCRIPT_NAME']));
  putenv('DOCUMENT_ROOT='.$_SERVER['DOCUMENT_ROOT']);
}

Now you can use $_SERVER[‘DOCUMENT_ROOT’] normally:

$docroot = getenv("DOCUMENT_ROOT"); 
include_once "$docroot/folder/yourfile.php"; 

Posted

in

,

by

Tags:

Comments

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.