(PHP 4 >= 4.1.0, PHP 5, PHP 7)
$_SERVER -- $HTTP_SERVER_VARS [removed] — Server and execution environment information
$_SERVER is an array containing information such as headers, paths, and script locations. The entries in this array are created by the web server. There is no guarantee that every web server will provide any of these; servers may omit some, or provide others not listed here. That said, a large number of these variables are accounted for in the » CGI/1.1 specification, so you should be able to expect those.
Note: Prior to PHP 5.4.0, $HTTP_SERVER_VARS contained the same initial information, but was not a superglobal. (Note that $HTTP_SERVER_VARS and $_SERVER were different variables and that PHP handled them as such.)
You may or may not find any of the following elements in $_SERVER. Note that few, if any, of these will be available (or indeed have any meaning) if running PHP on the command line.
CGI/1.1
'.
Note: Under Apache 2, you must set
UseCanonicalName = On
andServerName
. Otherwise, this value reflects the hostname supplied by the client, which can be spoofed. It is not safe to rely on this value in security-dependent contexts.
HTTP/1.0
';
GET
',
'HEAD
', 'POST
', 'PUT
'.
Note:
PHP script is terminated after sending headers (it means after producing any output without output buffering) if the request method was
HEAD
.
Accept:
header from the
current request, if there is one.
Accept-Charset:
header
from the current request, if there is one. Example:
'iso-8859-1,*,utf-8
'.
Accept-Encoding:
header
from the current request, if there is one. Example: 'gzip
'.
Accept-Language:
header
from the current request, if there is one. Example: 'en
'.
Connection:
header from
the current request, if there is one. Example: 'Keep-Alive
'.
Host:
header from the
current request, if there is one.
User-Agent:
header from
the current request, if there is one. This is a string
denoting the user agent being which is accessing the page. A
typical example is: Mozilla/4.5 [en] (X11; U;
Linux 2.2.9 i586). Among other things, you
can use this value with get_browser() to
tailor your page's output to the capabilities of the user
agent.
Note: Note that when using ISAPI with IIS, the value will be
off
if the request was not made through the HTTPS protocol.
Note: Your web server must be configured to create this variable. For example in Apache you'll need
HostnameLookups On
inside httpd.conf for it to exist. See also gethostbyaddr().
The absolute pathname of the currently executing script.
Note:
If a script is executed with the CLI, as a relative path, such as file.php or ../file.php, $_SERVER['SCRIPT_FILENAME'] will contain the relative path specified by the user.
80
';
using SSL, for instance, will change this to whatever your
defined secure HTTP port is.
Note: Under the Apache 2, you must set
UseCanonicalName = On
, as well asUseCanonicalPhysicalPort = On
in order to get the physical (real) port, otherwise, this value can be spoofed and it may or may not return the physical port value. It is not safe to rely on this value in security-dependent contexts.
Note: As of PHP 4.3.2, PATH_TRANSLATED is no longer set implicitly under the Apache 2 SAPI in contrast to the situation in Apache 1, where it's set to the same value as the SCRIPT_FILENAME server variable when it's not populated by Apache. This change was made to comply with the CGI specification that PATH_TRANSLATED should only exist if PATH_INFO is defined. Apache 2 users may use
AcceptPathInfo = On
inside httpd.conf to define PATH_INFO.
/index.html
'.
/some/stuff
.
Version | Description |
---|---|
5.4.0 | $HTTP_SERVER_VARS isn't available anymore due to the removal of long arrays registering. |
5.3.0 | Directive register_long_arrays which caused $HTTP_SERVER_VARS to be available has been deprecated. |
Example #1 $_SERVER example
<?php
echo $_SERVER['SERVER_NAME'];
?>
The above example will output something similar to:
www.example.com
Note:
This is a 'superglobal', or automatic global, variable. This simply means that it is available in all scopes throughout a script. There is no need to do global $variable; to access it within functions or methods.