The superglobal $ _SERVER array

One of the most important predefined arrays is the array $ _SERVER - in it the PHP interpreter places the variables received from the server. 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.

Comment

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.

$ _SERVER ['DOCUMENT_ROOT']

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".

Element $ _SERVER ['HTTP_ACCEPT']

The element $ _SERVER ['HTTP_ACCEPT'] describes the client's preferences regarding 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/vnd.ms-excel, 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 the 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.

Comment

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 less preferred types to more preferred ones. Using several parameters q, allows the client to tell the server the relative degree of preference for a particular media type.

Comment

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

Comment

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.

Element $ _SERVER ['HTTP_ACCEPT_LANGUAGE']

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. 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 agrees to accept 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.

www.sofftime.ru

Element $ _SERVER ['HTTP_REFERER']

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

Index.php page

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

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

Page page.php

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

Element $ _SERVER ['HTTP_USER_AGENT']

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 views the page using Internet Explorer version 6.0. The "Windows NT 5.1" line indicates that Windows XP is used as the operating system.

Comment

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, 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 to Internet Explorer there is 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.

Comment

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 useful for collecting statistical information, which allows designers to optimize the pages for the most common browsers.

Element $ _SERVER ['REMOTE_ADDR']

The $ _SERVER ['REMOTE_ADDR'] element contains the IP address of the client. When testing on the local machine - this address will be equal to 127.0.0.1. However, when testing in a 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.

Comment

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 true IP address of the client, such servers do not return the HTTP_X_FORWARDED_FOR environment variable.

Extracting the environment variable HTTP_X_FORWARDED_FOR

<?php
echo getenv ( HTTP_X_FORWARDED_FOR );
?>

Element $ _SERVER ['SCRIPT_FILENAME']

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, on a UNIX-like operating system the path is specified from the root directory /, for example "/var/share/www/test/index.php".

Element $ _SERVER ['SERVER_NAME']

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

www.softtime.ru

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 elements of the $ _SERVER array

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

Element $ _SERVER ['REQUEST_METHOD']

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

<?php
echo $_SERVER [ 'REQUEST_METHOD' ]; // GET
?>

Element $ _SERVER ['QUERY_STRING']

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

http://www.mysite.ru/test/index.php?id=1&test=wet&id_theme=512

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 the page with the entered text.

<?php
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

http://www.mysite.ru/test/index.php?id=1&test=wet&id_theme=512

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'].

Element $ _SERVER ['REQUEST_URI']

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:

http://www.mysite.ru/test/index.php?id=1&test=wet&id_theme=512

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

Full address of the script

<?php
echo "http://" . $_SERVER [ 'SERVER_NAME' ]. $_SERVER [ 'REQUEST_URI' ];
?>


1
178.20.156.230, 178.20.156.230, 66.102.9.136www.shram.kiev.uawww.shram.kiev.uahttp: //www.shram.kiev.ua/site/server.shtml
Array (36) {["REDIRECT_gzip-only-text / html"] => string (1) "1" ["REDIRECT_RF"] => string (39) "/home/admin/data/www/shram.kiev. Ua / site "[" REDIRECT_STATUS "] => string (3)" 200 "[" gzip-only-text / html "] => string (1)" 1 "[" HTTP_HOST "] => string (17)" Www.shram.kiev.ua "[" HTTP_X_FORWARDED_FOR "] => string (44)" 178.20.156.230, 178.20.156.230, 66.102.9.136 "[" HTTP_X_FORWARDED_PROTO "] => string (4)" http "[" HTTP_X_REAL_IP " ] => String (12) "66.102.9.136" ["HTTP_CONNECTION"] => string (5) "close" ["HTTP_ACCEPT"] => string (3) "* / *" ["HTTP_ACCEPT_CHARSET"] => string (7) "utf-8, *" ["HTTP_VIA"] => string (34) "1.0 translate.google.com 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 (29)" / sbin: / bin: / Usr / sbin: / usr / bin "[" SERVER_SIGNATURE "] => string (120)"
Apache / 2.2.24 (FreeBSD) mod_python / 3.3.1 Python / 2.7.5 PHP / 5.4.15 Server at www.shram.kiev.ua Port 80
"[" SERVER_SOFTWARE "] => string (64)" Apache / 2.2.24 (FreeBSD) mod_python / 3.3.1 Python / 2.7.5 PHP / 5.4.15 "[" SERVER_NAME "] => string (17)" www .shram.kiev.ua "[" SERVER_ADDR "] => string (14)" 178.20.156.230 "[" SERVER_PORT "] => string (2)" 80 "[" REMOTE_ADDR "] => string (12)" 66.102 .9.136 "[" DOCUMENT_ROOT "] => string (34)" /home/admin/data/www/shram.kiev.ua "[" SERVER_ADMIN "] => string (23)" [email protected] " ["SCRIPT_FILENAME"] => string (44) "/home/admin/data/www/shram.kiev.ua/index.php" ["REMOTE_PORT"] => string (5) "20311" ["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 (1489154661.241) [" REQUEST_TIME "] => int (1489154661)}
1.0 translate.google.com TWSFE / 0.9


NO