Beginner-Friendly Topic
Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!
Lesson 2 of 8
$_GET, $_POST & $_REQUEST - Reading User Input
$_GET and $_POST are superglobal arrays PHP automatically populates with request data. NEVER trust them - all data from the user is tainted until you validate and sanitise it. Reading and validating user input correctly is one of the most important security skills in PHP.
Reading & Validating Request Data
<?php
declare(strict_types=1);
// ── $_GET - query string parameters ───────────────────────────
// URL: /posts?page=2&category=php&sort=views
$page = isset($_GET["page"]) ? (int) $_GET["page"] : 1;
$category = $_GET["category"] ?? "";
$sort = $_GET["sort"] ?? "created_at";
// ✅ Safer: filter_input - reads and validates in one call
$page = filter_input(INPUT_GET, "page", FILTER_VALIDATE_INT, [
"options" => ["default" => 1, "min_range" => 1, "max_range" => 1000]
]);
$category = filter_input(INPUT_GET, "category", FILTER_SANITIZE_SPECIAL_CHARS);
// ── $_POST - form/body data (after POST submission) ───────────
// Always check request method before reading POST
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$title = trim($_POST["title"] ?? "");
$content = trim($_POST["content"] ?? "");
$email = trim($_POST["email"] ?? "");
}
// ── $_SERVER - server & request information ──────────────────
// ✅ Only use specific keys - never expose full $_SERVER
$isPost = $_SERVER["REQUEST_METHOD"] === "POST";
$isAjax = ($_SERVER["HTTP_X_REQUESTED_WITH"] ?? "") === "XMLHttpRequest";
$baseUrl = ($_SERVER["HTTPS"] ?? "off") === "on"
? "https://" . $_SERVER["HTTP_HOST"]
: "http://" . $_SERVER["HTTP_HOST"];
// ── Validation with filter_var ─────────────────────────────────
function validatePostInput(array $data): array {
$errors = [];
// Title validation
$title = trim($data["title"] ?? "");
if (empty($title)) {
$errors["title"] = "Title is required.";
} elseif (mb_strlen($title) > 200) {
$errors["title"] = "Title must be 200 characters or fewer.";
}
// Email validation
$email = trim($data["email"] ?? "");
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
$errors["email"] = "A valid email address is required.";
}
// Integer in range
$categoryId = filter_var($data["category_id"] ?? 0, FILTER_VALIDATE_INT, [
"options" => ["min_range" => 1]
]);
if ($categoryId === false) {
$errors["category_id"] = "Invalid category.";
}
return $errors;
}
// ── Process form submission ────────────────────────────────────
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$errors = validatePostInput($_POST);
if (empty($errors)) {
// Safe to proceed
$title = htmlspecialchars(trim($_POST["title"]), ENT_QUOTES, "UTF-8");
$content = trim($_POST["content"]);
// Save to database (Module 6)...
header("Location: /posts?success=1");
exit;
}
// Re-display form with errors
}Quick Quiz
Tip
Tip
Practice GET POST REQUEST Reading User Input in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
Visual architecture and concepts for $_GET, $_POST & $_REQUEST - Reading User Input
Practice Task
Note
Practice Task - (1) Write a working example of GET POST REQUEST Reading User Input from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Common Mistake
Warning
A common mistake with GET POST REQUEST Reading User Input is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready php code.
Finished this lesson?