Superglobals are built-in arrays available everywhere in your script.
| Variable | Contains |
|---|---|
$_GET |
values from the URL: page.php?id=5 |
$_POST |
values from a form sent with method="post" |
$_REQUEST |
GET + POST + cookies (avoid — be specific) |
$_SERVER |
request and server info |
$_SESSION |
data saved for the current visitor |
$_COOKIE |
cookies sent by the browser |
$_FILES |
uploaded files |
$_ENV |
environment variables |
Useful $_SERVER keys
$_SERVER['REQUEST_METHOD']—GETorPOST$_SERVER['REQUEST_URI']— the current URL path$_SERVER['HTTP_USER_AGENT']— browser information$_SERVER['REMOTE_ADDR']— visitor IP address
Golden rule
Everything in $_GET, $_POST, $_COOKIE and $_FILES comes from the user. Never trust it: validate it, and escape it with htmlspecialchars() before printing.
Example
Example — Reading URL values and request info
PHP
<?php
// Open as: superglobals.php?course=php&page=2
$course = $_GET['course'] ?? 'html';
$page = (int) ($_GET['page'] ?? 1);
echo 'Course: ' . htmlspecialchars($course) . '<br>';
echo "Page: $page<br>";
echo 'Method: ' . $_SERVER['REQUEST_METHOD'] . '<br>';
echo 'URL: ' . htmlspecialchars($_SERVER['REQUEST_URI']) . '<br>';
echo 'Your IP: ' . $_SERVER['REMOTE_ADDR'] . '<br>';
?>
<a href="?course=css&page=3">Open CSS, page 3</a>
Output: Course: php / Page: 2 / Method: GET / URL: /superglobals.php?course=php&page=2 / Your IP: 127.0.0.1 (on localhost). Clicking the link changes the values.