This page has been robot translated, sorry for typos if any. Original content here.

$ _SERVER superglobal array

One of the most important predefined arrays is the $ _SERVER array — the PHP interpreter places the variables received from the server into it. Without these variables, it is difficult to organize full support for Web applications. Below is a description of the most important elements of the $ _SERVER superglobal array.


You can view the full list of $ _SERVER array elements either by using the print_r () function, which prints the array dump, or by using the phpinfo () function, which displays information about the PHP interpreter.


The element $ _SERVER ['DOCUMENT_ROOT'] contains the path to the root directory of the server; if the script is executed in a virtual host, this element indicates the path to the root directory of the virtual host. Those. in the httpd.conf configuration file, the virtual host has the DocumentRoot directive, which is assigned the value "D: / main", the element $ _SERVER ['DOCUMENT_ROOT'] will contain the value "D: main".


The $ _SERVER ['HTTP_ACCEPT'] element describes client preferences regarding the type of document. The content of this element is retrieved from the Accept HTTP header, which sends the client to the server. The contents of this title may look like this.

image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, application/, application/msword, */*

The Accept header allows you to specify the media type that the client prefers to receive in response to his request. This header allows the server to be informed that the response is limited to a small number of preferred types.

The * symbol is used to group types in a media row. For example, the * / * symbol specifies the use of all types, and the type / * designation defines the use of all subtypes of the selected type.


Media types are separated from each other by commas.

Each media row is also characterized by an additional set of parameters. One of them is the so-called relative preference coefficient q, which takes values ​​from 0 to 1, respectively, from less preferred types to more preferred ones. Using multiple q parameters allows the client to inform the server of the relative degree of preference for a particular media type.


By default, the q parameter takes the value 1. In addition, it is separated from the media type by a semicolon.

Example of Accept Type Header:

Accept: audio/*; q=0.2, audio/basic

In this title, the first type is audio / *, which includes all musical documents and is characterized by a preference ratio of 0.2. A comma indicates the type of audio / basic for which the preference coefficient is not specified and accepts a default value equal to one. Quoting RFC2616, this title can be interpreted as follows: “I prefer the audio / basic type, but I can also send documents of any other audio-type, if they are available, after reducing the preference ratio by more than 80%”.

An example may be more complicated.

Accept: text/plain; q=0.5, text/html,
text/x-dvi; q=0.8, text/xc


Note that the $ _SERVER ['HTTP_ACCEPT'] element contains exactly the same information, but without the initial Accept header.

This header is interpreted as follows: The text / html and text / xc document types are preferred, but if they are not available, then the client sending this request will prefer text / x-dvi, and if it is not, then it can accept the text / type. plain.


The $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] element describes client preferences regarding language. This information is extracted from the Accept-Language HTTP header, which is sent by the client to the server. You can give the following example:

Accept-Language: ru, en; q=0.7

Which can be interpreted as follows: the client prefers the Russian language, but in case of his absence he agrees to accept documents in English. The element $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] will contain exactly the same information, but without the Accept-Language header:

ru, en; q=0.7

The content of the element $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] can be used to determine the nationality of visitors. However, the results will be approximate, as many users use English versions of browsers that will notify the server that the visitor prefers only one language - English.

Element $ _SERVER ['HTTP_HOST']

The $ _SERVER ['HTTP_HOST'] element contains the server name, which usually matches the domain name of the site located on the server. As a rule, the name specified in this parameter is the same as $ _SERVER ['SERVER_NAME']. The parameter contains only the domain name without a protocol name (http: //), i.e.


The $ _SERVER ['HTTP_REFERER'] element contains the address of the page from which the visitor came to this page. The transition should be carried out by reference. Create two pages index.php and page.php.

Index.php page

echo "<a href=page.php>Ссылка на страницу PHP</a><br />" ;
"Содержимое $_SERVER['HTTP_REFERER'] - " .

The page.php page will have similar content, but the link will point to the index.php page.

Page.php page

echo "<a href=index.php>Ссылка на страницу PHP</a><br />" ;
"Содержимое $_SERVER['HTTP_REFERER'] - " .

When moving from one page to another, the link to the page from which the transition was made will be displayed below the link.


The element $ _SERVER ['HTTP_USER_AGENT'] contains information about the type and version of the browser and the visitor's operating system.

Here is the typical content of this line: "Mozilla / 4.0 (compatible; MSIE 6.0; Windows NT 5.1)". The presence of the substring "MSIE 6.0" means that the visitor views the page using Internet Explorer version 6.0. The line "Windows NT 5.1" reports that Windows XP is used as the operating system.


For Windows 2000, the element $ _SERVER ['HTTP_USER_AGENT'] looks like this: "Mozilla / 4.0 (compatible; MSIE 5.01; Windows NT 5.0) ')", while for Windows XP - "Mozilla / 4.0 (compatible; MSIE 6.0 ; Windows NT 5.1) ".

If the visitor uses the Opera browser, the content of $ _SERVER ['HTTP_USER_AGENT'] might look like this: "Mozilla / 4.0 (compatible; MSIE 5.0; Windows 98) Opera 6.04 [ru]". The substring "MSIE 6.0" is also present here, reporting that Opera's browser is compatible with Internet Explorer and uses the same Windows dynamic libraries. Therefore, when analyzing the string returned by the browser, it should be borne in mind that Internet Explorer includes a string containing the substring "MSIE 6.0" and not containing the substring "Opera". In addition, from this line we can conclude that the user uses the Windows 98 operating system.


The Firefox browser user agent might look like Mozilla / 5.0 (Windows; U; Windows NT 5.1; en-US; rv: 1.8) Gecko / 20051111 Firefox / 1.5.

When using the Netscape browser, the contents of the element $ _SERVER ['HTTP_USER_AGENT'] might look like this: "Mozilla / 5.0 (X11; U; Linux i686; en-US; rv: 1.4) Gecko / 20030624 Netscape / 7.1". Belonging to this browser can be identified by the presence of the substring "Netscape". In addition, you can find out that the visitor is accessing the Internet using the operating version of Linux, with the kernel optimized for Pentium IV, being in the X-Window GUI. This mechanism is convenient to use for collecting statistical information, which allows designers to optimize pages for the most common browsers.


The $ _SERVER ['REMOTE_ADDR'] element contains the client's IP address. When testing on a local machine, this address will be However, when testing on a network, the variable will return the IP address of the client or the last proxy server through which the client reached the server. If the client uses a proxy server, its IP address can be obtained using the HTTP_X_FORWARDED_FOR environment variable, the value of which can be obtained using the getenv () function.


Proxy servers are special intermediate servers that provide a special type of service: traffic compression, data encoding, adaptation for mobile devices, etc. Among the many proxy servers, there are so-called anonymous proxy servers, which allow you to hide the true IP address of the client, such servers do not return the HTTP_X_FORWARDED_FOR environment variable.

Extracting the HTTP_X_FORWARDED_FOR environment variable

echo getenv ( HTTP_X_FORWARDED_FOR );


The element $ _SERVER ['SCRIPT_FILENAME'] contains the absolute path to the file from the root of the disk. So, if the server is running under the Windows operating system, then such a path might look like "d: main estindex.php", i.e. the path is specified from the disk; in a UNIX-like operating system, the path is specified from the root directory /, for example "/var/share/www/test/index.php".


The $ _SERVER ['SERVER_NAME'] element contains the server name, which is usually the same as the domain name of the site located on it. For example,

The content of the $ _SERVER ['SERVER_NAME'] element often matches the content of the $ _SERVER ['HTTP_HOST'] element. In addition to the server name, the $ _SERVER superglobal array allows you to find out a number of server parameters, such as the server's IP address, the listening port, which Web server is installed, and the HTTP protocol version. This information is placed in $ _SERVER ['SERVER_ADDR'], $ _SERVER ['SERVER_PORT'], $ _SERVER ['SERVER_SOFTWARE'] and $ _SERVER ['SERVER_PROTOCOL'] elements, respectively. The following is an example using these elements.

Using elements of the $ _SERVER array

echo "Имя сервера - " . $_SERVER [ 'SERVER_NAME' ]. "<br />" ;
"IP-адрес сервера - " . $_SERVER [ 'SERVER_ADDR' ]. "<br />" ;
"Порт сервера - " . $_SERVER [ 'SERVER_PORT' ]. "<br />" ;
"Web-сервер - " . $_SERVER [ 'SERVER_SOFTWARE' ]. "<br />" ;
"Версия HTTP-протокола - " . $_SERVER [ 'SERVER_PROTOCOL' ]. "<br />" ;


The $ _SERVER ['REQUEST_METHOD'] element contains the request method that is used to invoke the script: GET or POST.



The $ _SERVER ['QUERY_STRING'] element contains the parameters passed to the script if the query string is an address

then the $ _SERVER ['QUERY_STRING'] element will contain all the text after the "?". For example, when referring to the script shown below, placing arbitrary text in the query string after the "?" get the page with the entered text.

echo $_SERVER [ 'QUERY_STRING' ]; // id=1&test=wet&id_theme=512

Element $ _SERVER ['PHP_SELF']

The $ _SERVER ['PHP_SELF'] element contains the name of the script, starting from the root directory of the virtual host, i.e. if the query string is an address

then the element $ _SERVER ['PHP_SELF'] will contain the fragment "/test/index.php". As a rule, the same fragment is placed in the $ _SERVER ['SCRIPT_NAME'] element.


The element $ _SERVER ['REQUEST_URI'] contains the name of the script, starting from the root directory of the virtual host and parameters, i.e. if the query string is the address:

then the element $ _SERVER ['REQUEST_URI'] will contain the fragment "/test/index.php?id=1&test=wet&id_theme=512". In order to restore the full address in the script, which is placed in the query string, it is enough to use the combination of the elements of the $ _SERVER array presented below

Full address to the script

echo "http://" . $_SERVER [ 'SERVER_NAME' ]. $_SERVER [ 'REQUEST_URI' ];

one,, //
array (36) {["REDIRECT_gzip-only-text / html"] => string (1) "1" ["REDIRECT_RF"] => string (42) "/ var / www / admin / data / www / shram. "[" REDIRECT_STATUS "] => string (3)" 200 "[" gzip-only-text / html "] => string (1)" 1 "[" HTTP_HOST "] => string (17 ) "" ["HTTP_X_FORWARDED_FOR"] => string (40) ",," ["HTTP_X_FORWARDED_PROTO"] => string (4) "http" [" HTTP_X_REAL_IP "] => string (12)" "[" HTTP_CONNECTION "] => string (5)" close "[" HTTP_ACCEPT "] => string (3)" * / * "[" HTTP_ACCEPT_CHARSET "] = > string (7) "utf-8, *" ["HTTP_VIA"] => string (34) "1.0 TWSFE / 0.9" ["HTTP_USER_AGENT"] => string (60) "Mozilla / 4.0 ( compatible; MSIE 6.0; Windows NT 5.1), gzip (gfe) "[" HTTP_ACCEPT_ENCODING "] => string (15)" gzip, deflate, br "[" PATH "] => string (28)" / usr / local / bin: / usr / bin: / bin "[" SERVER_SIGNATURE "] => string (78)"
Apache / 2.2.22 (Debian) Server at Port 80
"[" SERVER_SOFTWARE "] => string (22)" Apache / 2.2.22 (Debian) "[" SERVER_NAME "] => string (17)" "[" SERVER_ADDR "] => string (12) "" ["SERVER_PORT"] => string (2) "80" ["REMOTE_ADDR"] => string (12) "" ["DOCUMENT_ROOT"] => string (37) " /var/www/admin/data/www/ "[" SERVER_ADMIN "] => string (23)" "[" SCRIPT_FILENAME "] => string (47)" / var / www / admin / data / www / / index.php "[" REMOTE_PORT "] => string (5)" 39380 "[" REDIRECT_QUERY_STRING "] => string (34)" & MY_REQUEST_URI = / site /server.shtml "[" REDIRECT_URL "] => string (18)" /site/server.shtml "[" GATEWAY_INTERFACE "] => string (7)" CGI / 1.1 "[" SERVER_PROTOCOL "] => string (8 ) "HTTP / 1.0" ["REQUEST_METHOD"] => string (3) "GET" ["QUERY_STRING"] => string (34) "& MY_REQUEST_URI = / site / server.shtml" ["REQUEST_URI"] => string ( 18) "/site/server.shtml" ["SCRIPT_NAME"] => string (10) "/index.php" ["PHP_SELF"] => string (10) "/index.php" ["REQUEST_TIME_FLOAT"] = > float (1544714653.616) ["REQUEST_TIME"] => int (15447 14653)}
1.0 TWSFE / 0.9