PHP Superglobals
Superglobals were enabled after PHP 4.1.0; they are built-in variables in the PHP system and are available in all scopes of a script.
PHP Superglobals
PHP predefines several superglobals, which means they are available in all scopes of a script. You can use them in functions and classes without any special declaration.
List of PHP superglobals:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
In this chapter we will explain several commonly used superglobals; the rest will be introduced in the following chapters.
PHP $GLOBALS
$GLOBALS is a PHP superglobal group that can be accessed in all scopes of a PHP script.
$GLOBALS is a global combined array containing all variables. The variable name is the key of the array.
The following example shows how to use the superglobal $GLOBALS:
Example
Run Example »
In the example above, z is a superglobal in the $GLOBALS array, which can also be accessed outside the function.
PHP $_SERVER
$_SERVER is an array containing information such as header, path, and script locations. The items in this array are created by the web server. There is no guarantee that every server provides all items; a server may omit some, or provide some that are not listed here.
The following example shows how to use the elements in $_SERVER:
Example
" ; echo $_SERVER [ ' SERVER_NAME ' ] ; echo "" ; echo $_SERVER [ ' HTTP_HOST ' ] ; echo "
" ; echo $_SERVER [ ' HTTP_REFERER ' ] ; echo "
" ; echo $_SERVER [ ' HTTP_USER_AGENT ' ] ; echo "
" ; echo $_SERVER [ ' SCRIPT_NAME ' ] ; ?>
Run Example »
The table below lists the important elements in all $_SERVER variables:
| Element/Code | Description |
|---|---|
| $_SERVER['PHP_SELF'] | The filename of the currently executing script, relative to the document root. For example, using $_SERVER['PHP_SELF'] in a script at the address http://example.com/test.php/foo.bar will yield /test.php/foo.bar.FILE A constant containing the full path and filename of the current (e.g. included) file. Starting from PHP 4.3.0, if PHP runs in command-line mode, this variable will contain the script name. In earlier versions this variable is not available. |
| $_SERVER['GATEWAY_INTERFACE'] | The version of the CGI specification used by the server; for example, "CGI/1.1". |
| $_SERVER['SERVER_ADDR'] | The IP address of the server where the current script is running. |
| $_SERVER['SERVER_NAME'] | The hostname of the server where the current script is running. If the script runs in a virtual host, this name is determined by the value set by that virtual host. (e.g.: www.runoob.com) |
| $_SERVER['SERVER_SOFTWARE'] | The server identification string, given in the header when responding to a request. (e.g.: Apache/2.2.24) |
| $_SERVER['SERVER_PROTOCOL'] | The name and version of the communication protocol used when requesting the page. For example, "HTTP/1.0". |
| $_SERVER['REQUEST_METHOD'] | The request method used to access the page; for example, "GET", "HEAD", "POST", "PUT". |
| $_SERVER['REQUEST_TIME'] | The timestamp when the request started. Available since PHP 5.1.0. (e.g.: 1377687496) |
| $_SERVER['QUERY_STRING'] | The query string, if any, through which the page is accessed. |
| $_SERVER['HTTP_ACCEPT'] | The content of the Accept: item in the current request header, if present. |
| $_SERVER['HTTP_ACCEPT_CHARSET'] | The content of the Accept-Charset: item in the current request header, if present. For example: "iso-8859-1,*,utf-8". |
| $_SERVER['HTTP_HOST'] | The content of the Host: item in the current request header, if present. |
| $_SERVER['HTTP_REFERER'] | The address of the previous page that referred the user agent to the current page (if it exists). Determined by the user agent setting. Not all user agents set this item, and some provide the ability to modify HTTP_REFERER. In short, this value is not reliable.) |
| $_SERVER['HTTPS'] | If the script is accessed via the HTTPS protocol, it is set to a non-empty value. |
| $_SERVER['REMOTE_ADDR'] | The IP address of the user browsing the current page. |
| $_SERVER['REMOTE_HOST'] | The hostname of the user browsing the current page. DNS reverse resolution does not depend on the user's REMOTE_ADDR. |
| $_SERVER['REMOTE_PORT'] | The port number used on the user's machine to connect to the Web server. |
| $_SERVER['SCRIPT_FILENAME'] | The absolute path of the currently executing script. |
| $_SERVER['SERVER_ADMIN'] | This value indicates the SERVER_ADMIN parameter in the Apache server configuration file. If the script runs on a virtual host, this value is that of the virtual host. (e.g.: someone@runoob.com) |
| $_SERVER['SERVER_PORT'] | The port used by the Web server. The default value is "80". If an SSL secure connection is used, this value is the HTTP port set by the user. |
| $_SERVER['SERVER_SIGNATURE'] | A string containing the server version and virtual host name. |
| $_SERVER['PATH_TRANSLATED'] | The base path of the current script's location in the file system (not the document root). This is the result after the server maps virtual to real paths. |
| $_SERVER['SCRIPT_NAME'] | Contains the path of the current script. This is very useful when a page needs to point to itself.FILE A constant containing the full path and filename of the current script (e.g. an included file). |
| $_SERVER['SCRIPT_URI'] | The URI used to specify the page to access. For example "/index.html". |
PHP $_REQUEST
PHP $_REQUEST is used to collect data submitted via HTML forms.
The example below shows a form with an input field and a submit button. When the user submits the form data by clicking the "Submit" button, the form data is sent to