R
Rishtaara
PHP Fundamentals
Lesson 11 of 32Article18 min

Superglobals — $_GET, $_POST, $_SESSION & More

Superglobals are built-in arrays available everywhere in your script. They carry request data, server info, cookies, files, and session state.

What are superglobals?

Superglobals are built-in arrays available everywhere in your script. They carry request data, server info, cookies, files, and session state.

Real-life example: Superglobals are airport baggage tags — every handler along the way can read the same tag without you passing bags manually.

  • $_GET — data from URL query string (?id=5)
  • $_POST — form data sent with POST method
  • $_REQUEST — GET + POST + COOKIE (avoid for clarity)
  • $_SERVER — headers, method, script path
  • $_COOKIE — browser cookies
  • $_SESSION — server-side session data
  • $_FILES — uploaded file metadata
  • $_ENV — environment variables

$_GET and $_POST

Use $_GET for search filters and page IDs. Use $_POST for login forms and data that should not sit in the URL. Always validate and sanitize.

Real-life example: $_GET is shouting your order across the room. $_POST is handing a sealed envelope to the cashier.

Reading GET and POST safely
<?php
// URL: page.php?id=5
$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);

if ($_SERVER["REQUEST_METHOD"] === "POST") {
  $name = trim($_POST["name"] ?? "");
  if ($name !== "") {
    echo "Hello, " . htmlspecialchars($name);
  }
}
?>

$_SERVER and $_FILES overview

$_SERVER['REQUEST_METHOD'] tells GET or POST. $_SERVER['HTTP_USER_AGENT'] shows browser info. $_FILES holds upload temp paths and sizes.

Real-life example: $_SERVER is the receipt printer at checkout — it records time, register number, and payment method.

Useful $_SERVER keys
<?php
echo $_SERVER["REQUEST_METHOD"];
echo $_SERVER["PHP_SELF"];
echo $_SERVER["HTTP_HOST"];

// After file upload form:
// $_FILES["photo"]["name"], ["tmp_name"], ["size"], ["error"]
?>