$_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:
- 'INDEX' represents the name of the superglobal element to be accessed, it must be written in uppercase and within quotes.
For example, to display the domain name where the script runs:
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 https://coursesweb.net/php-mysql/index.php, $_SERVER['PHP_SELF'] will return /php-mysql/index.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:126.96.36.199) 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 https://marplo.net/index.php, $_SERVER['SERVER_NAME'] will return marplo.net
- 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 https://coursesweb.net/index.php?id=23, $_SERVER['SCRIPT_NAME'] will return /index.php
- REQUEST_URI - The URI which was given in order to access this page
For example, if the address of the php script is https://marplo.net/index.php?id=23, $_SERVER['REQUEST_URI'] will return: /index.php?id=23
- Let's see some examples with some of this $_SERVER variables.
1. Get the visitor's ip
$ip = $_SERVER['REMOTE_ADDR'];
2. Get directory name from the URL address of the php script
// for example, if the URL address of the php script is: https://marplo.net/php-mysql/index.php?id=28
$adr = $_SERVER['PHP_SELF'];
$dir = dirname($adr);
echo $dir; // /php-mysql
3. Get current page full URL in PHP
// check for https
$protocol = !empty($_SERVER['HTTPS']) ? 'https://' : 'http://';
// sets the full address
$url = $protocol. $_SERVER['HTTP_HOST']. $_SERVER['REQUEST_URI'];
- A complete list with the superglobal $_SERVER Array you can find at the oficial page: Superglobal $_SERVER