Introduction: Why Superglobals Matter
In PHP, superglobals are built-in variables that are always accessible—anywhere, anytime. These powerful variables let you:
-
Receive form data (
$_POST
,$_GET
) -
Access session variables (
$_SESSION
) -
Interact with server info (
$_SERVER
) -
Handle file uploads (
$_FILES
) -
And much more…
They’re essential for dynamic, interactive web applications. Without them, capturing user input or managing sessions would be tedious and unsafe.
What Are PHP Superglobals?
PHP Superglobals are predefined associative arrays that are globally accessible, regardless of scope (inside functions, methods, or scripts). You don’t need to declare them with global
.
Here are the main PHP superglobals:
Superglobal | Purpose |
---|---|
$_GET |
Handles form data sent via URL (query string) |
$_POST |
Handles form data sent via POST request |
$_REQUEST |
Combines $_GET , $_POST , and $_COOKIE |
$_SERVER |
Contains server and environment info |
$_SESSION |
Stores session data for a user |
$_COOKIE |
Holds data from client-side cookies |
$_FILES |
Handles uploaded files |
$_ENV |
Contains environment variables |
$GLOBALS |
Access global variables from anywhere |
Understanding Each Superglobal with Examples
1. $_GET
– Access URL Parameters
// URL: page.php?name=Vinay
echo $_GET['name']; // Outputs: Vinay
Used in search queries, filters, and simple data transfer.
2. $_POST
– Handle Form Submissions Securely
// On form submit
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
echo $_POST['email'];
}
Ideal for sensitive data like passwords or large payloads.
3. $_REQUEST
– Combine GET, POST, COOKIE
$name = $_REQUEST['name']; // Tries POST, then GET, then COOKIE
Use cautiously—it can lead to ambiguity.
4. $_SERVER
– Server and Request Info
echo $_SERVER['HTTP_USER_AGENT']; // Shows user's browser info
echo $_SERVER['REQUEST_METHOD']; // GET, POST, etc.
Used for routing, debugging, or detecting client details.
5. $_SESSION
– Maintain User State
session_start();
$_SESSION['user'] = 'Vinay';
echo $_SESSION['user']; // Outputs: Vinay
Essential for login systems and user-specific data.
6. $_COOKIE
– Access Browser-Stored Data
echo $_COOKIE['language']; // e.g., en, fr, etc.
Cookies persist data across sessions but can be tampered with.
7. $_FILES
– Handle File Uploads
if ($_FILES['profile']['error'] === 0) {
move_uploaded_file($_FILES['profile']['tmp_name'], 'uploads/' . $_FILES['profile']['name']);
}
Use this with HTML <input type="file">
.
8. $_ENV
– Access Environment Variables
echo $_ENV['APP_ENV']; // Might return 'development'
Use for deployment configs or feature toggles.
9. $GLOBALS
– Access Global Variables in Any Scope
$siteName = "StudyZone";
function getName() {
echo $GLOBALS['siteName']; // Outputs: StudyZone
}
getName();
Useful, but overuse leads to tightly coupled code.
Complete Example: Login Form Using $_POST
and $_SESSION
<?php
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = $_POST['username'];
// Simulated login
if ($username === 'admin') {
$_SESSION['user'] = $username;
echo "Welcome, " . $_SESSION['user'];
} else {
echo "Invalid user!";
}
}
?>
<form method="POST">
<input type="text" name="username" required>
<button type="submit">Login</button>
</form>
Tips & Common Pitfalls
✅ Best Practices
-
Always sanitize user input from
$_GET
,$_POST
, and$_REQUEST
. -
Use
filter_input()
orhtmlspecialchars()
to avoid XSS. -
Prefer
$_POST
over$_REQUEST
for form submissions. -
Start sessions with
session_start()
before using$_SESSION
. -
Always check
$_FILES['error']
before processing files.
Common Mistakes
-
Accessing
$_SESSION
without starting session. -
Trusting raw
$_GET
input without validation. -
Using
$_REQUEST
carelessly (can introduce security bugs). -
Forgetting enctype in file upload forms:
<form enctype="multipart/form-data">
Superglobal Comparison Chart
Superglobal | Read-Only | Purpose | Scope | Secure? |
---|---|---|---|---|
$_GET |
❌ | URL query parameters | Global | ❌ |
$_POST |
❌ | Form data (POST) | Global | ✅ |
$_REQUEST |
❌ | Merged GET/POST/COOKIE | Global | ❌ |
$_SERVER |
✅ | Server environment data | Global | ✅ |
$_SESSION |
❌ | Persist user state | Global | ✅ |
$_COOKIE |
❌ | Read browser cookies | Global | ❌ |
$_FILES |
❌ | Uploaded file data | Global | ✅ |
$_ENV |
✅ | Environment variables | Global | ✅ |
$GLOBALS |
❌ | Access global variables | Global | ❌ |
✅ Conclusion: Superglobals in a Nutshell
PHP superglobals are indispensable for dynamic web development. They bridge the gap between user input, server environment, and application logic.
Key Takeaways:
-
Use the right superglobal for the task:
$_POST
for forms,$_SESSION
for login,$_SERVER
for request info. -
Always validate and sanitize input.
-
Avoid using
$_REQUEST
unless absolutely necessary. -
Understand security implications of
$_COOKIE
,$_GET
.
By mastering superglobals, you can create secure, robust, and interactive PHP applications.