Theory
Forms & Superglobals
PHP 8.3.8
Web Features

Forms & Superglobals

Topic 9 of 13
PHP
<?php
// Superglobals
$_GET;      // URL query params
$_POST;     // Form POST data
$_SERVER;   // Server/env info
$_SESSION;  // Session data
$_COOKIE;   // Cookie data
$_FILES;    // Uploaded files

// Safe form handling
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $name  = htmlspecialchars(trim($_POST['name'] ?? ''));
    $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);
    $age   = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);

    if ($email === false) {
        echo "Invalid email!";
    }
}

// Sessions
session_start();
$_SESSION['user_id'] = 42;
session_destroy();

// Cookies
setcookie('theme', 'dark', time() + 86400 * 30);
โš ๏ธ Security
NEVER echo raw $_GET or $_POST โ€” always sanitize with htmlspecialchars() first. Use filter_input() to validate types.