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 "<br>_SERVER:<br><code>";
print_r($_SERVER);
echo "</code><br><br>_ENV:<br><code>";
print_r($_ENV);
echo "</code><br><br>";

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";

Pin It on Pinterest

Share This