LearnContact
Lesson 2920 min read

Superglobals

Explore PHP's built-in superglobal arrays like $_GET, $_POST, $_SERVER, and $_SESSION that are available everywhere in your script.

Introduction

Every time a browser sends a request to a PHP script, it carries a lot of information along with it: form data, URL parameters, cookies, uploaded files, and details about the server itself. PHP makes all of this available through a special set of built-in arrays called superglobals.

This lesson introduces the most important superglobals, what kind of data each one holds, and why you must always treat their contents as untrusted input rather than data you can rely on blindly.

What You Will Learn
  • What makes a variable a "superglobal" in PHP.
  • The purpose of $_GET, $_POST, $_SERVER, $_SESSION, $_COOKIE, and $_FILES.
  • What $_REQUEST combines and why it is used less often.
  • Why you should never trust superglobal data without validating or escaping it first.

What Are Superglobals?

Superglobals are built-in PHP arrays that are automatically available in every scope of your script — inside every function, every class method, everywhere — without needing the global keyword or any special setup.

Key Difference From Regular Globals

A regular global variable requires the global keyword to be visible inside a function. Superglobals skip that requirement entirely — they are visible everywhere, all the time, by design.

superglobal-visibility.php
<?php
function showRequestMethod() {
    // No "global" keyword needed - $_SERVER is always visible
    echo "Method: " . $_SERVER["REQUEST_METHOD"] . "\n";
}

showRequestMethod();
?>
Result
Method: GET

$_GET

$_GET is an associative array holding any values passed in the URL's query string — the part after the question mark, like ?id=42&sort=asc.

get-example.php
<?php
// Visiting page.php?id=42&category=books
echo "ID: " . $_GET["id"] . "\n";
echo "Category: " . $_GET["category"] . "\n";
?>
Result
ID: 42
Category: books

$_POST

$_POST is an associative array holding data submitted through an HTML form using the POST method. Unlike $_GET, POST data is sent in the request body rather than visible in the URL.

post-example.php
<?php
// After submitting a form with fields named "username" and "email"
if (isset($_POST["username"])) {
    echo "Username: " . $_POST["username"] . "\n";
    echo "Email: " . $_POST["email"] . "\n";
}
?>
Result
Username: alice
Email: alice@example.com

$_SERVER

$_SERVER holds information about the server environment and the current request: the request method, the requested URI, headers, and more.

server-example.php
<?php
echo "Request Method: " . $_SERVER["REQUEST_METHOD"] . "\n";
echo "Requested URI: " . $_SERVER["REQUEST_URI"] . "\n";
echo "Host: " . $_SERVER["HTTP_HOST"] . "\n";
?>
Result
Request Method: GET
Requested URI: /page.php?id=42
Host: www.example.com

$_SESSION

$_SESSION stores data that should persist for a single visitor across multiple page loads — commonly used to keep someone logged in as they navigate a site. Sessions require calling session_start() before they can be used, which you will cover in more detail in a later lesson.

session-example.php
<?php
session_start();

$_SESSION["username"] = "alice";

echo "Logged in as: " . $_SESSION["username"] . "\n";
?>
Result
Logged in as: alice

$_COOKIE holds small pieces of data the browser stores and sends back with every request to the same site — often used for remembering preferences or a visitor across separate browser sessions.

cookie-example.php
<?php
if (isset($_COOKIE["theme"])) {
    echo "Preferred theme: " . $_COOKIE["theme"] . "\n";
} else {
    echo "No theme preference saved yet.\n";
}
?>
Result
Preferred theme: dark

$_FILES

$_FILES holds information about files uploaded through an HTML form, including each file's original name, size, temporary location on the server, and any upload errors.

files-example.php
<?php
if (isset($_FILES["avatar"])) {
    echo "Uploaded file: " . $_FILES["avatar"]["name"] . "\n";
    echo "Size: " . $_FILES["avatar"]["size"] . " bytes\n";
}
?>
Result
Uploaded file: profile.jpg
Size: 204800 bytes

$_REQUEST

$_REQUEST is a combined array that typically includes values from $_GET, $_POST, and $_COOKIE all at once. It can be convenient, but because it does not tell you which source a value actually came from, most PHP developers prefer being explicit with $_GET or $_POST instead.

request-example.php
<?php
// Works whether "id" arrived via the URL or a form field
echo "ID: " . $_REQUEST["id"] . "\n";
?>
Result
ID: 42

Never Trust Superglobal Input

Every value inside $_GET, $_POST, $_COOKIE, $_FILES, and $_REQUEST originates from the visitor's browser — and browsers can be controlled directly by anyone, including people intentionally trying to break or exploit your application.

Security Reminder

Never trust superglobal data directly. Always validate that required fields exist and match the format you expect, and always escape any of this data before displaying it back on a page or using it in a database query. Treating user input as safe by default is one of the most common causes of real-world security vulnerabilities.

The next few lessons will build directly on this idea — you will learn how to process form submissions safely and validate user input before trusting it.

Common Mistakes

Avoid These Mistakes
  • Assuming a $_GET or $_POST value will always be set, and getting an undefined-index warning when it is not.
  • Echoing superglobal data directly into HTML without escaping it first.
  • Using $_REQUEST when you actually need to know whether data came from the URL or a form.
  • Forgetting that $_SESSION requires session_start() to be called before it can be used.

Best Practices

  • Check that a key exists with isset() before reading from a superglobal array.
  • Prefer $_GET and $_POST explicitly over $_REQUEST so your code's intent is clear.
  • Always validate and escape superglobal data before using or displaying it.
  • Keep $_SERVER values in mind as a source of request context, not just form data.

Frequently Asked Questions

Why are they called "superglobals" instead of just "globals"?

Because unlike ordinary global variables, they are automatically available inside every function and class method without needing the global keyword.

Can I create my own superglobal?

No, superglobals are a fixed, built-in set defined by PHP itself. You cannot register a new one.

Is $_REQUEST just a shortcut for $_GET and $_POST combined?

Essentially yes, and depending on server configuration it can include $_COOKIE data as well, which is part of why many developers avoid it in favor of being explicit.

Do I need $_FILES for every form?

No, only for forms that include a file upload input. Regular text fields go through $_POST or $_GET as usual.

Is it safe to display $_GET data directly on the page?

No. Any value from a superglobal should be escaped (for example with htmlspecialchars()) before being echoed into HTML, to prevent cross-site scripting attacks.

Key Takeaways

  • Superglobals are built-in PHP arrays automatically available in every scope of your script.
  • $_GET and $_POST hold data sent from URL query strings and submitted forms respectively.
  • $_SERVER, $_SESSION, $_COOKIE, and $_FILES expose server info, persistent session data, cookies, and uploaded files.
  • $_REQUEST combines several of these but is less explicit about where data came from.
  • Superglobal data comes from the visitor and must always be validated and escaped before it is trusted.

Summary

Superglobals are how PHP hands your script all the information that arrives with an HTTP request, from form fields to server details to uploaded files. They are available everywhere, which makes them convenient — but that same convenience means every value in them must be treated as untrusted until proven otherwise.

In this lesson, you toured the most important superglobals and why blind trust in them is dangerous. Next, you will put $_GET and $_POST to work by building and processing a real HTML form.

Lesson 29 Completed
  • You know what makes a superglobal different from a regular variable.
  • You can identify the purpose of $_GET, $_POST, $_SERVER, $_SESSION, $_COOKIE, and $_FILES.
  • You understand what $_REQUEST combines.
  • You understand why superglobal input must always be validated and escaped.
Next Lesson →

Forms Handling