English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
Superglobal variables in PHP 4.1The variables after .0 are enabled, which are built-in variables in the PHP system and are available in the entire scope of a script.
PHP has predefined several superglobal variables (superglobals), which means they are available in the entire scope of a script. You do not need to specify them to use them in functions and classes.
List of PHP superglobal variables:
$GLOBALS
$_SERVER
$_REQUEST
$_POST
$_GET
$_FILES
$_ENV
$_COOKIE
$_SESSION
In this chapter, we will discuss several commonly used superglobal variables, and the other variables will be introduced in the next few chapters.
$GLOBALS is a superglobal variable group in PHP, which can be accessed in the entire scope of a PHP script.
$GLOBALS is a combined array of all variables, which contains all variables. The variable name is the key of the array.
The following examples introduce how to use the superglobal variable $GLOBALS:
<?php $x = 75; $y = 25; function addition() { $GLOBALS['z'] = $GLOBALS['x']; + $GLOBALS['y']; } addition(); echo $z; ?>Test and see ‹/›
In the above examples, z is a superglobal variable in the $GLOBALS array, which can also be accessed outside of functions.
$_SERVER is an array that contains information such as headers, paths, and script locations, among others. This array is created by the web server. It cannot be guaranteed that every server provides all items; some may be ignored, or some may not be listed here.
The following examples demonstrate how to use elements in $_SERVER:
<?php echo $_SERVER['PHP_SELF']; echo "<br>"; echo $_SERVER['SERVER_NAME']; echo "<br>"; echo $_SERVER['HTTP_HOST']; echo "<br>"; echo $_SERVER['HTTP_REFERER']; echo "<br>"; echo $_SERVER['HTTP_USER_AGENT']; echo "<br>"; echo $_SERVER['SCRIPT_NAME']; ?>Test and see ‹/›
The following table lists all important elements of the $_SERVER variables:
Element/Code | Description |
---|---|
$_SERVER['PHP_SELF'] | The file name of the script being executed, related to the document root. For example, at the address http://example.com/test.php/Using $_SERVER['PHP_SELF'] in the script of foo.bar will get /test.php/foo.bar. The __FILE__ constant contains the complete path and file name of the current (for example, including) file. From PHP 4.3.0 version, if PHP is running in command-line mode, this variable will contain the script name. This variable is not available in earlier versions. |
$_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 is running in a virtual host, the name is determined by the value set by that virtual host. (For example: www.w3(codebox.com) |
$_SERVER['SERVER_SOFTWARE'] | The server identifier string, given in the header information when responding to the request. (For example: 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'] | Timestamp when the request started. From PHP 5.1.0 is available. (For example:1377687496) |
$_SERVER['QUERY_STRING'] | query string (query string), if any, for page access. |
$_SERVER['HTTP_ACCEPT'] | Current request header Accept item content, if it exists. |
$_SERVER['HTTP_ACCEPT_CHARSET'] | Current request header Accept-Charset item content in the current request header, if it exists. For example: "iso-8859-1,*,utf-8". |
$_SERVER['HTTP_HOST'] | The content of the Host item in the current request header, if it exists. |
$_SERVER['HTTP_REFERER'] | Directs the user agent to the previous page of the current page (if it exists). It is determined by the user agent settings. Not all user agents set this item, and some even provide the function to modify HTTP_REFERER. In short, this value is not trustworthy.) |
$_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 by the user's machine to connect to the web server. |
$_SERVER['SCRIPT_FILENAME'] | The absolute path of the script currently being executed. |
$_SERVER['SERVER_ADMIN'] | This value indicates the SERVER_ADMIN parameter in the Apache server configuration file. If the script is running on a virtual host, this value is that virtual host's value (for example: someone@w3(codebox.com) |
$_SERVER['SERVER_PORT'] | The port used by the web server. The default value is "8"0". If an SSL secure connection is used, this value is the HTTP port set by the user. |
$_SERVER['SERVER_SIGNATURE'] | A string that includes the server version and the virtual host name. |
$_SERVER['PATH_TRANSLATED'] | The basic path of the file system where the current script is located (not the document root directory). This is the result after the server performs a virtual to real path mapping. |
$_SERVER['SCRIPT_NAME'] | Contains the path of the current script. This is very useful when the page needs to point to itself. The __FILE__ constant contains the complete path and filename of the current script (for example, included files). |
$_SERVER['SCRIPT_URI'] | URI is used to specify the page to be accessed. For example "/"index.html". |
PHP $_REQUEST is used to collect data submitted from HTML forms.
The following example shows a form with an input field (input) and a submit button (submit): 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 handle the form data. If you want other PHP files to handle the data, you can change the specified script filename. Then, we can use the superglobal variable $_REQUEST to collect data from the input fields in the form:
<html> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];;"> Name: <input type="text" name="fname"> <input type="submit"> </form> <?php $name = $_REQUEST['fname']; echo $name; ?> </body> </html>Test and see ‹/›
PHP $_POST is widely used to collect form data, specified by the attribute "method="post" in the HTML form tag.
The following example shows a form with an input field (input) and a submit button (submit): 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 handle the form data. If you want other PHP files to handle the data, you can change the specified script filename. Then, we can use the superglobal variable $_POST to collect the data of the input fields in the form:
<html> <body> <form method="post" action="<?php echo $_SERVER['PHP_SELF'];;"> Name: <input type="text" name="fname"> <input type="submit"> </form> <?php $name = $_POST['fname']; echo $name; ?> </body> </html>Test and see ‹/›
PHP $_GET is widely used to collect form data, specified by the attribute "method="get" in the HTML form tag.
$_GET can also collect data sent in the URL.
Assuming we have an HTML page with parameters:
<html> <body> <a href="test_get.php?subject=PHP&web=oldtoolbag.com">Test $GET</a> </body> </html>
When the user clicks the link "Test $GET", the parameter "subject" and "web" will be sent to "test_get.php", and you can use the $_GET variable in the "test_get.php" file to retrieve this data.
The following example shows the code of the "test_get.php" file:
<html> <body> <?php echo "Study" . $_GET['subject'] . " @ " . $_GET['web']; ?> </body> </html>Test and see ‹/›
Tip: If you want to learn more about $_POST and $_GET, please visit our PHP Form Chapter.