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

Superglobal array $ _SERVER

One of the most important predefined arrays is the $ _SERVER array - the PHP interpreter puts the variables received from the server into it. Without these variables, it is difficult to organize full-fledged support for Web applications. The following are the most important elements of the $ _SERVER superglobal array.


You can see the full list of elements of the $ _SERVER array either using the function print_r (), which prints the dump of the array 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 runs in the virtual host, this element specifies the path to the root directory of the virtual host. Those. in the httpd.conf configuration file, the virtual host has a DocumentRoot directive that is set to "D: / main", the $ _SERVER ['DOCUMENT_ROOT'] element will contain the value of "D: main".


The $ _SERVER ['HTTP_ACCEPT'] element describes the client's preferences for the document type. The content of this element is retrieved from the HTTP Accept header, which the client sends to the server. The contents of this header 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 their request. This header allows the server to be informed that the response is limited to a small number of preferred types.

The * character is used to group types in a media series. For example, the * / * character specifies the use of all types, and type / * denotes the use of all subtypes of the selected type type.


Media types are separated from each other by commas.

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


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

Example of an Accept header:

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

In this header, the first type is audio / * including all musical documents and characterized by a preference factor of 0.2. The type of audio / basic is indicated via a comma, for which the preference factor is not specified and assumes a default value of one. Quoting RFC2616, this header can be interpreted as follows: "I prefer the type of audio / basic, but I can also send documents of any other audio type, if they are available, after reducing the preference factor by more than 80%."

The example can 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 prefers text / x-dvi, and if it is not, then it can take the type text / plain.


The $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] element describes the client's preferences for the language. This information is extracted from the Accept-Language HTTP header, which the client sends to the server. We 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 agrees to accept the documents in English. The $ _SERVER ['HTTP_ACCEPT_LANGUAGE'] element will contain exactly the same information, but without the Accept-Language header:

ru, en; q=0.7

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

Element $ _SERVER ['HTTP_HOST']

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


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

Index.php page

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

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

Page page.php

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

When moving from one page to another, the link will display the address of the page from which the transition was made.


The $ _SERVER ['HTTP_USER_AGENT'] element 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" indicates that the visitor browses the page using Internet Explorer version 6.0. The "Windows NT 5.1" line indicates that Windows XP is used as the operating system.


For Windows 2000, the $ _SERVER ['HTTP_USER_AGENT'] element 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, then the contents of $ _SERVER ['HTTP_USER_AGENT'] can 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, informing that Opera browser is compatible with the Internet Explorer browser and uses the same Windows dynamic libraries. Therefore, when analyzing the string returned by the browser, it should be borne in mind that the Internet Explorer includes a string containing the substring "MSIE 6.0" and not containing the substring "Opera". In addition, from this line you can conclude that the user is using the operating system Windows 98.


The Firefox 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 $ _SERVER ['HTTP_USER_AGENT'] element might look like this: "Mozilla / 5.0 (X11; U; Linux i686; en-US; rv: 1.4); Gecko / 20030624 Netscape / 7.1." Access to this browser can be determined by the presence of the substring "Netscape". In addition, you can find out that the visitor is going to the Internet using an operating version of Linux, with a kernel optimized for Pentium IV, being in the X-Window graphical environment. This mechanism is convenient to use to collect statistical information that allows designers to optimize the pages for the most common browsers.


The $ _SERVER ['REMOTE_ADDR'] element contains the IP address of the client. When testing on the local machine - this address will be equal to However, when testing on the network, the variable will return the IP address of the client or the last proxy server through which the client came to the server. If the client uses a proxy server, you can find out its IP address using the HTTP_X_FORWARDED_FOR environment variable, which can be retrieved using the getenv () function.


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

Extracting the environment variable HTTP_X_FORWARDED_FOR

echo getenv ( HTTP_X_FORWARDED_FOR );


The element $ _SERVER ['SCRIPT_FILENAME'] places the absolute path to the file from the root of the disk. So, if the server is running a Windows operating system, then this path might look like "d: main estindex.php", i.e. the path is specified from the disk, in the 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, 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 contents 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, for example, the server's IP address, the listening port, which Web server is installed, and the version of the HTTP protocol. This information is placed in the elements $ _SERVER ['SERVER_ADDR'], $ _SERVER ['SERVER_PORT'], $ _SERVER ['SERVER_SOFTWARE'] and $ _SERVER ['SERVER_PROTOCOL'], respectively. The following is an example using these elements.

Using array elements $ _SERVER

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, which is used to call the script: GET or POST.



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

then the element $ _SERVER ['QUERY_STRING'] will get all the text after the "?" sign. For example, when accessing the script presented below, placing in the query string any text after the "?" we get a page with the entered text.

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

Element $ _SERVER ['PHP_SELF']

The element $ _SERVER ['PHP_SELF'] 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". Typically, the same fragment is placed in the element $ _SERVER ['SCRIPT_NAME'].


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 an 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 elements of the array $ _SERVER, presented below

Full address of the script

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

1,, //
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 / "[" REMOTE_PORT "] => string (5)" 37088 "[" 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 (1537860981.692) ["REQUEST_TIME"] => int (15378 60981)}
1.0 TWSFE / 0.9