$_SERVER is an Array that stores information about the server PHP is running on, such as headers, paths, and script locations. The entries in this array are created by the web server. The $_SERVER Array is a 'superglobal' variable. This means that it is available in all scopes throughout a script, you can access it directly within functions or classes.
To access an element of the superglobal $_SERVER array you can use the fallowing syntax which will represent the domain name where the current script is executing:
Here are some of the most used variables of the superglobal $_SERVER array:
PHP_SELF- The filename of the currently executing script, relative to the document root. For example, if the address of the php script is http://www.phpwin.org/test.php, $_SERVER['PHP_SELF'] will return /test.php
REQUEST_METHOD- Which request method was used to access the page ('GET', 'HEAD', 'POST', 'PUT')
REQUEST_TIME- The timestamp of the start of the request. Available since PHP 5.1.0.
DOCUMENT_ROOT- The document root directory under which the current script is executing, as defined in the server's configuration file (i.e. /var/www/vhosts/domain.net/httpdocs ).
HTTPS- Set to a non-empty value if the script was queried through the HTTPS protocol.
HTTP_ACCEPT- Contents of the Accept: header from the current request, if there is one.
HTTP_ACCEPT_CHARSET- Contents of the Accept-Charset: header from the current request, if there is one (Example: 'iso-8859-1,*,utf-8').
HTTP_HOST- Contents of the Host: header from the current request, if there is one.
HTTP_REFERER- The address of the page (if any) which referred to the current page. This is set by the user agent.
HTTP_USER_AGENT- Contents of the User-Agent: header from the current request, if there is one. This is a string denoting the user agent being which is accessing the page (example: Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:22.214.171.124) Gecko/20101203 Firefox/3.6.13)
REMOTE_ADDR- The IP address of the user is viewing the current page.
SCRIPT_FILENAME- The absolute pathname of the currently executing script (i.e. /var/www/vhosts/domain.net/httpdocs/index.php )
SERVER_ADDR- The IP address of the server under which the current script is executing.
SERVER_NAME- The domain name where the current script is executing. For example, if the address of the php script is http://www.phpwin.org/index.php, $_SERVER['SERVER_NAME'] will return www.phpwin.org
SERVER_PROTOCOL- Name and revision of the information protocol via which the page was requested (i.e. HTTP/1.1)
SERVER_ADMIN- The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file.
SERVER_PORT- The port used by the web server for communication
SCRIPT_NAME- Contains the current script's path. This is useful for pages which need to point to themselves. For example, if the address of the php script is http://www.phpwin.org/search.php?q=var, $_SERVER['SCRIPT_NAME'] will return /search.php
REQUEST_URI- The URI which was given in order to access this page For example, if the address of the php script is http://www.phpwin.org/search.php?q=var, $_SERVER['REQUEST_URI'] will return: /search.php?q=var
Let's see some examples with some of this $_SERVER variables:
Get the visitor's ip
Get directory name from the URL address of the php script