- HubPages»
- Technology»
- Computers & Software»
- Computer Science & Programming»
- Programming Languages
Learn About PHP $_SERVER Variable/Array
Introduction
Despite of Having a Huge Library of PHP functions, PHP also have large number of predefined variables, which allows use to fetch information about current environment/state of PHP. This type of Information basically includes headers, error messages, cookies, URL parameters and their values etc. . In this list of predefined variables, there exist a variable which allow us to fetch information about Server and execution environment i.e. $_SERVER of which we are going discuss in this PHP tutorial. Here, Below is the list of contents with anchor links pointing to various sections of this tutorial.
Usage and Description
This array/variable ($_SERVER) is a replacement of deprecated $HTTP_SERVER_VARS variable in PHP, Which allows us to get information about server and Current execution environment of PHP script. This array/variable stores/contains information such as headers, paths, and script locations and is "superglobal" by default.
- About "Superglobal" Property - It Says/Refers that we are allowed to use this variable anywhere in our PHP Scripts without worrying about it's presence in a limited scope (It is Available in All scopes). We do not need to manually declare it as a global variable in the starting of our scripts.
Predefined Named Constants and their Description
Here, Below is the List of named constants (Indices) that can be used to fetch information from this array including description about each.
Indices of $_SERVER Array Variable
Named Constant
| Description
|
---|---|
'PHP_SELF'
| The Filename of Current PHP Page excluding the hostname from URL i.e. "http://example.com/php/phptest.php" will be "/php/phptest.php"
|
'argv'
| Array of arguments passed to the script. When the script is run on the command line, this gives C-style access to the command line parameters. When called via the GET method, this will contain the query string.
|
'argc'
| Contains the number of command line parameters passed to the script (if run on the command line).
|
'GATEWAY_INTERFACE'
| What revision of the CGI specification the server is using; i.e. 'CGI/1.1'.
|
'SERVER_ADDR'
| The IP address of the server under which the current script is executing.
|
'SERVER_NAME'
| The name of the server host under which the current script is executing. If the script is running on a virtual host, this will be the value defined for that virtual host.
|
'SERVER_SOFTWARE'
| Server identification string, given in the headers when responding to requests.
|
'SERVER_PROTOCOL'
| Name and revision of the information protocol via which the page was requested; i.e. 'HTTP/1.0';
|
'REQUEST_METHOD'
| Which request method was used to access the page; i.e. 'GET', 'HEAD', 'POST', 'PUT'.
|
'REQUEST_TIME'
| The timestamp of the start of the request. Available since PHP 5.1.0.
|
'QUERY_STRING'
| The query string, if any, via which the page was accessed.
|
'DOCUMENT_ROOT'
| The document root directory under which the current script is executing, as defined in the server's configuration file.
|
'HTTP_ACCEPT'
| Contents of the Accept: header from the current request, if there is one.
|
'HTTP_ACCEPT_CHARSET'
| Contents of the Accept-Charset: header from the current request, if there is one. Example: 'iso-8859-1,*,utf-8'.
|
'HTTP_ACCEPT_ENCODING'
| Contents of the Accept-Encoding: header from the current request, if there is one. Example: 'gzip'.
|
'HTTP_ACCEPT_LANGUAGE'
| Contents of the Accept-Language: header from the current request, if there is one. Example: 'en'.
|
'HTTP_CONNECTION'
| Contents of the Connection: header from the current request, if there is one. Example: 'Keep-Alive'.
|
'HTTP_HOST'
| Contents of the Host: header from the current request, if there is one.
|
'HTTP_REFERER'
| The address of the page (if any) which referred the user agent to the current page. This is set by the user agent. Not all user agents will set this, and some provide the ability to modify HTTP_REFERER as a feature. In short, it cannot really be trusted.
|
'HTTP_USER_AGENT'
| Contents of the User-Agent: header from the current request, if there is one.
|
'HTTPS'
| Set to a non-empty value if the script was queried through the HTTPS protocol.
|
'REMOTE_ADDR'
| The IP address from which the user is viewing the current page.
|
'REMOTE_HOST'
| The Host name from which the user is viewing the current page. The reverse dns lookup is based off the REMOTE_ADDR of the user.
|
'REMOTE_PORT'
| The port being used on the user's machine to communicate with the web server.
|
'SCRIPT_FILENAME'
| The absolute pathname of the currently executing script.
|
'SERVER_ADMIN'
| The value given to the SERVER_ADMIN (for Apache) directive in the web server configuration file. If the script is running on a virtual host, this will be the value defined for that virtual host.
|
'SERVER_PORT'
| The port on the server machine being used by the web server for communication. For default setups, this will be '80'; using SSL, for instance, will change this to whatever your defined secure HTTP port is.
|
'SERVER_SIGNATURE'
| String containing the server version and virtual host name which are added to server-generated pages, if enabled.
|
'PATH_TRANSLATED'
| Filesystem- (not document root-) based path to the current script, after the server has done any virtual-to-real mapping.
|
'SCRIPT_NAME'
| Contains the current script's path. This is useful for pages which need to point to themselves. The __FILE__ constant contains the full path and filename of the current (i.e. included) file.
|
'REQUEST_URI'
| The URI which was given in order to access this page; for instance, '/index.html'.
|
'PHP_AUTH_DIGEST'
| When doing Digest HTTP authentication this variable is set to the 'Authorization' header sent by the client (which you should then use to make the appropriate validation).
|
'PHP_AUTH_USER'
| When doing HTTP authentication this variable is set to the username provided by the user.
|
'PHP_AUTH_PW'
| When doing HTTP authentication this variable is set to the password provided by the user.
|
'AUTH_TYPE'
| When doing HTTP authenticated this variable is set to the authentication type.
|
'PATH_INFO'
| if the current script was accessed via the URL http://www.example.com/php/path_info.php/some/stuff?foo=bar, then $_SERVER['PATH_INFO'] would contain /some/stuff.
|
'ORIG_PATH_INFO'
| Original version of 'PATH_INFO' before processed by PHP.
|
Examples of $_SERVER
Here, Below I'm Publishing some examples that are showing the usage of this predefined PHP variable or Array.
- if you're running PHP script shown below on your own computer i.e. on server installed in your computer, then, it will output "localhost" or "127.0.0.1".
Example 1: showing simple usage of $_SERVER array variable
<?php echo $_SERVER['SERVER_NAME']; //Above PHP statement will output /* The Domain Name on which you're running this PHP script, for example: "www.example.com" */ ?>
Example 2 : PHP Code to Print All Data of $_SERVER into Tabular Format
<?php echo '<table border="1">'; foreach ($_SERVER as $k => $v){ echo "<tr><td>" . $k ."</td><td>" . $v . "</td></tr>"; } echo "</table>" ?>
Above PHP script will output the Similar Data as shown below, It varies from system to system as per the PHP configuration.
Output Will Be Similar To This
MIBDIRS
| C:/Program Files/xampp/php/extras/mibs
|
MYSQL_HOME
| \xampp\mysql\bin
|
OPENSSL_CONF
| C:/Program Files/xampp/apache/bin/openssl.cnf
|
PHP_PEAR_SYSCONF_DIR
| \xampp\php
|
PHPRC
| \xampp\php
|
TMP
| \xampp\tmp
|
HTTP_HOST
| localhost
|
HTTP_USER_AGENT
| Mozilla/5.0 (Windows NT 6.1; rv:6.0.2) Gecko/20100101 Firefox/6.0.2
|
HTTP_ACCEPT
| text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
|
HTTP_ACCEPT_LANGUAGE
| en-us,en;q=0.5
|
HTTP_ACCEPT_ENCODING
| gzip, deflate
|
HTTP_ACCEPT_CHARSET
| ISO-8859-1,utf-8;q=0.7,*;q=0.7
|
HTTP_CONNECTION
| keep-alive
|
HTTP_CACHE_CONTROL
| max-age=0
|
PATH
| C:\Program Files\Common Files\Microsoft Shared\Windows Live;C:\Program Files\PC Connectivity Solution\;C:\Program Files\NVIDIA Corporation\PhysX\Common;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Program Files\Windows Live\Shared;C:\Program Files\Common Files\Nero\Lib\
|
SystemRoot
| C:\Windows
|
COMSPEC
| C:\Windows\system32\cmd.exe
|
PATHEXT
| .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
|
WINDIR
| C:\Windows
|
SERVER_SIGNATURE
| Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1 Server at localhost Port 80
|
SERVER_SOFTWARE
| Apache/2.2.17 (Win32) mod_ssl/2.2.17 OpenSSL/0.9.8o PHP/5.3.4 mod_perl/2.0.4 Perl/v5.10.1
|
SERVER_NAME
| localhost
|
SERVER_ADDR
| 127.0.0.1
|
SERVER_PORT
| 80
|
REMOTE_ADDR
| 127.0.0.1
|
DOCUMENT_ROOT
| C:/Program Files/xampp/htdocs
|
SERVER_ADMIN
| postmaster@localhost
|
SCRIPT_FILENAME
| C:/Program Files/xampp/htdocs/online/phptest.php
|
REMOTE_PORT
| 51258
|
GATEWAY_INTERFACE
| CGI/1.1
|
SERVER_PROTOCOL
| HTTP/1.1
|
REQUEST_METHOD
| GET
|
QUERY_STRING
| |
REQUEST_URI
| /online/phptest.php
|
SCRIPT_NAME
| /online/phptest.php
|
PHP_SELF
| /online/phptest.php
|
REQUEST_TIME
| 1316617340
|
Ask for Help
If you still have queries regarding this function or about any section of this tutorial, then, you can ask about it in comments section below.