Environmental Variables
All environmental variables passed to a script are placed by the interpreter into the $HTTP_ENV_VARS array. The format of this array is different on different computers. In Windows, environmental variables can be checked by executing the set command in the command line; in UNIX-like systems, environmental variables can be viewed by executing the env command.
You can find the following PHP environmental variables of use:
- $DOCUMENT_ROOT — The path to the document root directory of the currently executing script on the server.
- $SCRIPT_FILENAME — The current script's path.
- $SERVER_ADDR – The address of the IP server on which the current script is executing.
- $SERVER_PORT — The server port used by the web server for communication.
- $SERVER_NAME — The server host name.
- $SERVER_PROTOCOL – The HTTP version.
- $REMOTE_ADDR – The IP address of the computer that requested the current script page.
- $REMOTE_PORT — The port on the remote machine used to communicate with the web server.
- $REQUEST_METHOD — The method (GET or POST) used to request the current script.
- $REQUEST_URI – The URL, but without the domain name or server address, given to access the current path. For example, if the URL in the request for the current page was given as http://192.168.1.1/admin/index.php, this variable will hold /admin/index.php.
- $QUERY_STRING — The query string containing the list of the parameters passed to the request. The parameters are given as parameter_name=parameter_value, and are delimited with the ampersand (&).
- $HTTP_HOST — The server name. This parameter may hold the same name as the $SERVER_NAME parameter, but may be different is the server has more than one name.
- $HTTP_USER_AGENT – A string identifying the user agent accessing the page. For example, the browser name, although this name does not always reflect the real name.
- $HTTP_ACCEPT — A list of files that can be processed by the client.
The following example displays some of the environmental variables:
<?php
print("<p>$DOCUMENT_ROOT</p>");
print("<p>$SCRIPT_FILENAME</p>");
print("<p>$HTTP_HOST</p>");
?>