PHP $_SERVER Variable

by Vincy. Last modified on July 8th, 2022.

$_SERVER is one of the superglobal variables in PHP. It contains information about headers, server, host and etc. For that, it contains a huge list of information with appropriate global indices.

With these global indices, the $_SERVER array contains values for the meta-variables listed with the specification of the common gateway interface. So, while running a PHP script, the $_SERVER array will be loaded with the values based on the criteria, like, which interface the server is running and etc.

Now, we are going to see some of the lists of possible indices with the $_SERVER array in PHP. And also, we are going to see the factors based on which these array indices can be categorized.

So, $_server array indices contain information related to the list of the following items.

  • Server information
  • Header information
  • Details on PHP page request
  • File name or path information
  • Remote user information
  • HTTP Authentication Details

Let us look into the available indices of the $_SERVER array, that falls under each of the above lists, one by one.

Server Information in PHP $_SERVER Array

  • GATEWAY_INTERFACE – It holds the name of the common gateway interface version under which the server is running. $_SERVER indices are loaded with the meta-variables as specified with this interface.
  • SERVER_NAME – It contains the server hostname (virtual hostname, if any).
  • SERVER_ADDR – contains a server IP address.
  • SERVER_PORT – port number of the server, which is 80 by default.
  • SERVER_SIGNATURE – A server-generated string will be loaded with this index when the server generates the page as the response.
  • SERVER_PROTOCOL – The name of the protocol using which the server is accessed.

$_SERVER Containing Header Information

Under this category, we are going to see the available indices of this PHP global array that are used to store information like accepted language and encoding technique, charset, connection and etc.

  • HTTP_HOST
  • HTTP_CONNECTION
  • HTTP_ACCEPT_CHARSET
  • HTTP_ACCEPT_ENCODING
  • HTTP_ACCEPT_LANGUAGE

All of the above indices of the PHP $_SERVER array will be loaded with the values stored in the header while requesting a page from the server. And the captions to refer to these values with header content, are Host, Connection, Charset, Encoding, and Language, respectively.

  • HTTP_REFERER – For this array index, the current page IP address will be set by the user agent, but not guaranteed.
  • HTTPS – It can be loaded with any value if and only if the SERVER_PROTOCOL holds value as HTTPS; meaning that, if we request a server page by using HTTPS protocol, then, this index will be set.

Added to that, some other array indices in this category, which are used to hold details such as accepted MIME type, user agent and etc.

Details on PHP Page Request

The $_server array contains indices that hold information about page requests as shown below.

  • REQUEST_METHOD – This will contain values from any one of GET or POST methods, using which the request is submitted to the server.
  • REQUEST_TIME – This will represent the time in the form of the timestamp, on which the page request is sent.

File name or path information

The indices coming under this category will be used widely to create a full URL for the current page. For that, we can concatenate strings stored into the $_SERVER array with the following array indices.

  • PHP_SELF – It holds a relative URL of the currently executing page with respect to the document root.
  • DOCUMENT_ROOT – holds the name of the root directory as specified in the PHP configuration file php.ini.
  • QUERY_STRING – It holds the arguments passed with page URL after? mark. For example, if a page URL is like, http://localhost/php_samples/index.php?action=”default”, then, this index will contain action=default.
  • REQUEST_URI – It contains an identifier used to access the page resource.
  • SCRIPT_FILENAME – This contains the full path of the page running currently.
  • PATH_INFO – It consists of information if anything is given on the client-side. This information will be added in between the absolute URL and the URL parameters passed.

Remote User Information Accessing PHP Page from Server

This category includes a set of array indices that are used to store the name, address, and port of the remote user host, from which the page request is generated. And the corresponding indices are as follows.

  • REMOTE_HOST
  • REMOTE_ADDR
  • REMOTE_PORT

And then, it also contains indices for storing authenticated users to access pages or to redirect. These array indices are,

  • REMOTE_USER
  • REDIRECT_REMOTE_USER

HTTP Authentication Details

HTTP authentication is used to let the user enter the username and password to access the requested page. Once, the user entered their username and password, it will be stored into the global variables, $_SERVER[“PHP_AUTH_USER”], $_SERVER[“PHP_AUTH_PW”], respectively.

HTTP authentication can be any one of basic or digest-type authentication. If we choose digest type, then, the $_SERVER[“PHP_AUTH_DIGEST”] will be set. And, $_SERVER[“PHP_AUTH_TYPE”] is used to store the type of authentication selected.

Vincy
Written by Vincy, a web developer with 15+ years of experience and a Masters degree in Computer Science. She specializes in building modern, lightweight websites using PHP, JavaScript, React, and related technologies. Phppot helps you in mastering web development through over a decade of publishing quality tutorials.

Comments to “PHP $_SERVER Variable”

Leave a Reply

Your email address will not be published. Required fields are marked *

↑ Back to Top

Share this page