The superglobals are available throughout your script. Superglobals include form data sent from your visitor, cookie data, session information, local server information, and more.
There are nine superglobal arrays available shown in the following table.
Variable | Meaning |
---|---|
$_GET | Contains all variables sent via a HTTP GET request. For example, a URL of myfile.php?name=Paul would load myfile.php and give you $_GET["name"] with the value "Paul". |
$_POST | Contains all variables sent via a HTTP POST request. |
$_FILES | Contains all variables sent via a HTTP POST file upload. |
$_COOKIE | Contains all variables sent via HTTP cookies. |
$_REQUEST | Contains all variables sent via HTTP GET, HTTP POST, and HTTP cookies. This is the equivalent of combining $_GET, $_POST, and $_COOKIE. |
$_SESSION | Contains all variables stored in a user's session. |
$_SERVER | Contains all variables set by the web server. |
$_ENV | Contains all environment variables. |
$GLOBALS | An array containing all global variables in your script. |
The most commonly used $_SERVER
variables are shown in the following table.
Only PHP_SELF is available on the command line.
Name | Value |
---|---|
HTTP_REFERER | If the user clicked a link to get the current page, this will contain the URL of the previous page, or it will be empty if the user entered the URL directly. |
HTTP_USER_AGENT | The name reported by the visitor's web browser. |
PATH_INFO | Any data passed in the URL after the script name. |
PHP_SELF | The name of the current script. |
REQUEST_METHOD | Either GET or POST. |
QUERY_STRING | Includes everything after the question mark in a GET request. Not available on the command line. |
HTTP_REFERER
and HTTP_USER_AGENT
can find out
a lot about your visitor and then take the appropriate
action.
For example:
<?php if (isset($_SERVER['HTTP_REFERER'])) { print "The page you were on previously was {$_SERVER['HTTP_REFERER']}<br />"; } else { print "You didn't click any links to get here<br />"; } ?> <a href="refer.php">Click me!</a>
The code above generates the following result.
The PATH_INFO
element in $_SERVER
allows you to grab directory information specified after the script.
Consider this script:
<?PHP if (isset($_SERVER['PATH_INFO'])) { print "The page you requested was {$_SERVER['PATH_INFO']}<br />"; } else { print "You didn't request a page<br />"; } ?>
The code above generates the following result.
Save the code above as pathinfo.php, then load it in your web browser. Edit the URL, adding a file name to the end of pathinfo.php. Such as, yoursite.com/pathinfo.php/path/file.txt. Load the page to see extra path information.