agyanadda@gmail.com

PHP Superglobals

PHP Forms and Web 1 min read Updated Dec 1, 2026 Topic 21 of 36

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'] — GET or POST
  • $_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.

Test Yourself With Exercises

Exercise 1 Multiple Choice

Which superglobal holds values from the URL query string?

Exercise 2 True / False

Values in $_POST can be trusted because they come from your own form.

Exercise 3 Fill in the Blank

$_SERVER['REQUEST'] tells whether the request is GET or POST.

Frequently Asked Questions

It turns any input into a safe whole number, so ?page=abc becomes 0 instead of breaking your query or page.