Tuesday, 21 July 2015

PHP $_SERVER Variable

$_SERVER is one of the super global variables in PHP. It contains information about headers, server, host and etc. For that, it contains huge list of information with appropriate global indices.
With these global indices, $_SERVER array contains values for the meta variables listed with the specification of common gateway interface. So, while running a PHP script, $_SERVER array will be loaded with the values based on the criteria, like, in which interface the server is running and etc.
Now, we are going to see some of the list of possible indices with $_SERVER array in PHP. And also, we are going to see about the factors based on which these array indices can be categorized.
So, $_server array indices contain information related to the list of 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 $_SERVER array, that falls under each of the above list, one by one.
php_server_array

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 server host name (virtual host name, if any).
  • SERVER_ADDR – contains 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 page as 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 PHP $_SERVER array will be loaded with the values stored in header, while requesting a page from server. And the captions to refer these values with header content, are Host, Connection, Charset, Encoding, Language, respectively.
  • HTTP_REFERER – For this array index, 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_PROTOCOLholds value as https; meaning that, if we request server page by using https protocol, then, this indices 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

$_SERVER array contains indices that hold information about page request is 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 timestamp, on which the page request is sent.
php_server_array_indices

File name or path information

The indices coming under this category will be used widely to create 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 currently executing page with respect to the document root.
  • DOCUMENT_ROOT – holds name of the root directory as specified in 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 identifier used to access the page resource.
  • SCRIPT_FILENAME – Contains full path of the page running currently.
  • PATH_INFO – It consists information if anything given at the client side. This information will be added in between absolute URL and the URL parameters passed.

Remote User Information Accessing PHP Page from Server

This category includes set of array indices that are used to store 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 user to access page or to redirect. These array indices are,
  • REMOTE_USER
  • REDIRECT_REMOTE_USER

HTTP Authentication Details

HTTP authentication is used to let user to enter the user name and password to access to requested page. Once, the user entered their user name 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 as selected.

0 comments:

Post a Comment