Using Environment Variables in PHP
Environment variable definition
PHP environment variables allow your scripts to glean certain types of data dynamically from the server. This supports script flexibility in a potentially changing server environment. For example, the SITE_HTMLROOT variable provided by (mt) Media Temple will automatically provide the correct path to your document root on any (gs) Grid-Service server, without necessitating any changes in your script. (mt) Media Temple provides several dozen variables like this for your convenience.
READ ME FIRST
This article is provided as a courtesy. Installing, configuring, and troubleshooting third-party applications is outside the scope of support provided by (mt) Media Temple. Please take a moment to review the Statement of Support.
Use and examples
You can access these variables using the $_SERVER and $_ENV arrays.
For example, if you want to use the SITE_HTMLROOT variable mentioned above, you can create a variable in your PHP script similar to the following:
Filename: environment.php
$site_path_var = $_SERVER["SITE_HTMLROOT"];
This will create a variable with a value similar to the following:
/home/00000/domains/example.com/html
If you want to test the output of the variable, add an echo statement to your PHP script. For example:
Filename: environment.php
$site_path_var = $_SERVER["SITE_HTMLROOT"];
echo $site_path_var;
Now visit your script in your browser to see the output.
NOTE:
$_SERVER and $_ENV are PHP superglobal arrays. They do not have to be declared as global variables. Do NOT put the following line in your file:
Filename: environment.php
global $_SERVER;
Practical use
On the (gs) Grid-Service, it is particularly helpful to use a few select environment variables, for three reasons. First, if you ever decide to re-use a script on a different domain on the (gs) Grid-Service, you won't have to change all your variables. Second, the variables are often more convenient than the long path and server names used for the (gs) Grid-Service architecture. Third, the (gs) Grid-Service load-balancing occasionally requires sites and databases to be shifted to different physical machines, which can change your environment. This is not a common occurrence, but it is an essential part of (gs) Grid-Service functionality.
-
$_SERVER["SITE_HTMLROOT"]
The full path to your site's document root, returns output like /home/00000/domains/example.com/html. -
$_ENV["DATABASE_SERVER"]- The internal database server name, returns output like internal-db.s00000.gridserver.com.
Complete list of provided variables
Create a phpinfo.php page to view all of your (mt) Media Temple-provided variables. See our "How can I create a phpinfo.php page?" article for details, paying attention the PHP Variables section of the page for the relevant information.
Setting your own variables
In PHP
On the (gs) Grid-Service, you can set your own environment variables that will last within the session that created them. For example, if you want to use a custom environment variable in a script, you can add the following lines to create and then use a variable:
Filename: environment.php
$_ENV["MYENV"]="new_variable";
$new_variable_var = $_ENV["MYENV"];
Note: These environment variables will not last outside the session in which they were created.
In .htaccess
You can also have Apache set environment variables for use in your scripts, via a .htaccess file, using SetEnv or in Rewrite rules. These variables must start with 'HTTP_' for security purposes.
SetEnv HTTP_MY_VARIABLE "my value"
User-friendly server information
If you want to view system information in a more user-friendly format, check your (gs) Grid-Service Server Guide. The Database Connections and System Paths sections contain useful information about your server environment.
