Skip to content

PHP Tutorial Superglobals

Why Superglobals Matter

Lesson 155 of 369 2 min read By Deepak Paswan Updated Oct 5, 2026

Superglobals are the doorway between the outside world and your PHP code.

SuperglobalWho controls it
$_GET, $_POST, $_REQUESTThe visitor
$_COOKIEThe visitor
$_FILESThe visitor (name, type, content)
$_SERVER (HTTP_* keys)The visitor
$_SERVER (other keys), $_ENVThe server
$_SESSIONYour code (stored on the server)

Almost everything that goes wrong with web security starts with trusting a value from the top half of that table.

Three habits

1. Read with a default.

index.php
PHP
<?php
$_GET = [];

$page = $_GET["page"] ?? 1;

echo $page;
Output
OUTPUT
1

2. Validate before you use.

index.php
PHP
<?php
$_GET = ["page" => "-4"];

$page = filter_var($_GET["page"] ?? 1, FILTER_VALIDATE_INT, ["options" => ["min_range" => 1, "default" => 1]]);

echo $page;
Output
OUTPUT
1

3. Escape when you output.

index.php
PHP
<?php
$_GET = ["name" => "<b>Bob</b>"];

echo "Hello, " . htmlspecialchars($_GET["name"] ?? "");
Output
OUTPUT
Hello, &lt;b&gt;Bob&lt;/b&gt;

One place to read input

When every page reads $_POST directly, mistakes spread. A small helper keeps the rules in one spot.

index.php
PHP
<?php
final class Input
{
    public function __construct(private array $data) {}

    public function string(string $key, string $default = ""): string
    {
        $value = $this->data[$key] ?? $default;

        return is_string($value) ? trim($value) : $default;
    }

    public function int(string $key, int $default = 0): int
    {
        $value = filter_var($this->data[$key] ?? null, FILTER_VALIDATE_INT);

        return $value === false ? $default : $value;
    }
}

$input = new Input(["name" => "  Isha ", "age" => "29x"]);

echo "[" . $input->string("name") . "] ";
echo $input->int("age", -1);
Output
OUTPUT
[Isha] -1

Frameworks such as Laravel give you a “request” object that works like this, so the habit carries over.

Do not write into superglobals

Changing $_GET or $_POST makes code hard to follow. Copy the value into a variable and change the variable.

filter_input()

filter_input() reads straight from the original request and validates in one step.

index.php
PHP
<?php
$id = filter_input(INPUT_GET, "id", FILTER_VALIDATE_INT);   // int, false or null

It returns null when the variable is missing and false when the value fails the filter. That is easy to forget, so many developers prefer a helper like the one above.

lightbulb
Summary
Superglobals are untrusted by default. Read them with defaults, validate them against a rule, and escape them for the place they are printed.