Today, I was faced with the need to redefine a PHP $_SERVER variable for an entire application, made up of hundreds of PHP pages. In this case, I needed to redefine $_SERVER['DOCUMENT_ROOT'] for an entire website. The application was built by another company under the assumption that $_SERVER['DOCUMENT_ROOT'] would always point to the document root, however, on some virtual servers this is not the case. The solution actually proved to be very easy, and could be used to redefine other PHP $_SERVER variables as required.
Solution:
Step 1:
Create a PHP file, in this case we are naming it document_root.php. The PHP file should contain the following code (Make sure to replace our path with your path to your document root):
<?php $_SERVER["DOCUMENT_ROOT"] = "/usr/local/www/htdocs/domainname.com/www"; ?> |
Step 2:
Add the following code at the top of your .htaccess file (Make sure to replace the document_root.php with your file. If your file is nested in a directory include the file path as well):
php_value auto_prepend_file "document_root.php" |
Reload your website and poof, Apache will parse the document_root.php file and set the $_SERVER['DOCUMENT_ROOT'] value on every page you load.
Note: If you are redefining a different global variable or multiple global variables you can revise the PHP file accordingly.
Leave A Comment