Superglobals are built-in array variables in PHP that are always available in all scopes throughout a script without requiring global $variable; declarations.
| Superglobal | Purpose |
|---|---|
$_SERVER |
Contains web server headers, paths, script locations, & request IP |
$_GET |
Holds variables passed in the URL query string (?id=5) |
$_POST |
Holds variables submitted via HTTP POST requests |
$_FILES |
Holds uploaded file metadata and temporary storage paths |
$_COOKIE |
Holds HTTP cookie variables sent by browser |
$_SESSION |
Holds session state data across multiple requests |
$_REQUEST |
Contains contents of $_GET, $_POST, and $_COOKIE combined |
$_ENV |
Holds environment variables set on the web server |
$GLOBALS |
References all global variables available in global scope |
flowchart LR
A["HTTP Request"] --> B["Web Server Processing"]
B --> C["$_SERVER (Headers & IP)"]
B --> D["$_GET (Query Params)"]
B --> E["$_POST / $_FILES (Body & Uploads)"]
B --> F["$_COOKIE (Browser Cookies)"]
C & D & E & F --> G["PHP Application Logic"]
<?php
declare(strict_types=1);
// Inspect Server & Client Request Metadata
$clientIP = $_SERVER['REMOTE_ADDR'] ?? '127.0.0.1';
$requestMethod = $_SERVER['REQUEST_METHOD'] ?? 'GET';
$requestURI = $_SERVER['REQUEST_URI'] ?? '/';
$userAgent = $_SERVER['HTTP_USER_AGENT'] ?? 'Unknown';
// Safely Retrieve URL Query Parameter (?search=laravel)
$searchTerm = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS) ?? '';
echo "=== Request Audit Log ===
";
echo "HTTP Method: $requestMethod
";
echo "Request URI: $requestURI
";
echo "Client IP: $clientIP
";
echo "Search Term Parameter: '$searchTerm'
";
filter_input() or explicit sanitization routines before using them.$_REQUEST: Explicitly inspect $_GET or $_POST depending on expected HTTP verb to prevent CSRF and parameter pollution vulnerabilities.filter_input(INPUT_POST, 'key') over Direct $_POST['key']: Protects against undefined array key notices when variables are missing.Write a code snippet that uses filter_input(INPUT_GET, 'page', FILTER_VALIDATE_INT) to safely retrieve an integer page parameter with a default fallback of 1!
Sign in to track your learning journey, earn industry-recognized certificates, and join our elite developer community.
Quick Access With
Experiment with the code from this lesson in our interactive playground.
You've completed this section! Take a quick 5-question quiz to check your understanding.