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 has several predefined superglobals, which means they are available in all scopes of a script. You do not need to declare anything special to use them in functions and classes.
List of PHP superglobals:
- $GLOBALS
- $_SERVER
- $_REQUEST
- $_POST
- $_GET
- $_FILES
- $_ENV
- $_COOKIE
- $_SESSION
In this chapter, we will cover several commonly used superglobals. The remaining variables will be introduced in the following chapters.
PHP $GLOBALS
$GLOBALS is a superglobal variable group in PHP that can be accessed in all scopes of a PHP script.
$GLOBALS is a global combined array containing all variables. The names of the variables are the keys of the array.
The following example demonstrates how to use the superglobal variable $GLOBALS:
Example
Run example »
In the above example, z is a superglobal variable in the $GLOBALS array, and this variable can also be accessed outside the function.
PHP $_SERVER
$_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 server provides all entries; the server may ignore some, or provide some that are not listed here.
The following example demonstrates how to use the elements in $_SERVER:
Example
Run example »
The table below lists the important elements in the $_SERVER variable:
| 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 return /test.php/foo.bar. The __FILE__ constant contains the full path and filename of the current (e.g., included) file. As of PHP 4.3.0, if PHP is running in command-line mode, this variable will contain the script name. In earlier versions, this variable was unavailable. |
| $_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 currently running script is located. |
| $_SERVER['SERVER_NAME'] | The hostname of the server where the currently running script is located. If the script runs in a virtual host, this name is determined by the value set by that virtual host. (e.g., www.example.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 at the start of the request. Available as of PHP 5.1.0. (e.g., 1377687496) |
| $_SERVER['QUERY_STRING'] | The query string, if any, through which the page was accessed. |
| $_SERVER['HTTP_ACCEPT'] | The content of the Accept: header in the current request, if present. |
| $_SERVER['HTTP_ACCEPT_CHARSET'] | The content of the Accept-Charset: header in the current request, if present. For example: "iso-8859-1,*,utf-8". |
| $_SERVER['HTTP_HOST'] | The content of the Host: header in the current request, if present. |
| $_SERVER['HTTP_REFERER'] | The address of the previous page that guided the user agent to the current page (if it exists). Determined by the user agent's settings. Not all user agents set this item, and some even provide the ability to modify HTTP_REFERER. In short, this value should not be trusted. |
| $_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 on the user's machine used 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 virtual host's value. (e.g., [email protected]) |
| $_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 user-configured HTTP port. |
| $_SERVER['SERVER_SIGNATURE'] | A string containing the server version and the virtual host name. |
| $_SERVER['PATH_TRANSLATED'] | The base path of the filesystem (not the document root) where the current script is located. This is the result after the server maps virtual to real paths. |
| $_SERVER['SCRIPT_NAME'] | The path of the current script. This is very useful when the page needs to point to itself. The __FILE__ constant contains the full path and filename of the current script (e.g., included files). |
| $_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 by HTML forms.
The following example displays 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 will be sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to process the form data. If you want another PHP file to process the data, you can modify the specified script filename. Then, we can use the superglobal variable $_REQUEST to collect the input field data from the form:
Example
Run example »
PHP $_POST
PHP $_POST is widely used to collect form data by specifying the attribute "method="post"" in the HTML form tag.
The following example displays 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 will be sent to the script file specified in the action attribute of the <form> tag. In this example, we specify the file to process the form data. If you want another PHP file to process the data, you can modify the specified script filename. Then, we can use the superglobal variable $_POST to collect the input field data from the form:
Example
Run example »
PHP $_GET
PHP $_GET is also widely used to collect form data by specifying the attribute "method="get"" in the HTML form tag.
$_GET can also collect data sent in the URL.
Suppose we have an HTML page with a hyperlink that contains parameters:
<html> <body> <a href="test_get.php.html">Test $GET</a> </body> </html>
When the user clicks the link "Test $GET", the parameters "subject" and "web" will be sent to "test_get.php". You can use the $_GET variable in the "test_get.php" file to retrieve these data.
The following example shows the code of the "test_get.php" file:
Example
Run example »
Tip:If you want to learn more about $_POST and $_GET, please visit ourPHP Formschapter.
Other Extensions