Using Environment Variables in PHP
This article explains the basics of using environment variables in PHP scripts to maximize portability.
(mt) Media Temple platforms provide many useful environment variables specific to each user account. These are available to the PHP scripting language, and their use can greatly increase the portability and maintainability of your scripts.
The associative array $_SERVER is defined for all PHP scripts, and holds these environment variables. For example, the environment variable that holds path of your site's html root is called SITE_HTMLROOT. To use this from within a PHP script, you could do the following:
$site_path = $_SERVER["SITE_HTMLROOT"];
Please note that $_SERVER is one of PHP's superglobal arrays, which means that it is defined in every scope. For example, the following is incorrect:
function my_function() {
global $_SERVER;
$site_path = $_SERVER["SITE_HTMLROOT"];
...
}
You do not ever have to declare $_SERVER to be global. The following would be the correct implementation:
function my_function() {
$site_path = $_SERVER["SITE_HTMLROOT"];
...
}
For a complete listing of the available values in the $_SERVER array, create a file called phpinfo.php somewhere in your site.
Look at this page from a web browser and scroll down to the section called "PHP Variables." This table has a complete listing of the $_SERVER variables available.
Using these values in your scripts will ensure portability if your environment is upgraded, your site is migrated to a new server, or your domain name changes.
If you are on our (gs) Grid-Service platform see the server guide for more info:
- Log into your AccountCenter
- Click on Admin
- Click on Server Guide
- Click on System Paths
Notes/Supplemental Resources:
Revisions:
07-20-2009: Minor Fixes
Fields marked with an asterisk(*) are required. Comment on this article