User Registration

Key Techniques Used

The Validation Pattern

<?php
$errors = [];

if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $username = trim($_POST['username'] ?? '');

    // Validation rules
    if (empty($username)) {
        $errors['username'] = 'Username is required';
    } elseif (strlen($username) < 3) {
        $errors['username'] = 'Username must be at least 3 characters';
    }

    // Process only if no errors
    if (empty($errors)) {
        // Save to database, send email, etc.
        header('Location: success.php');
        exit;
    }
}
?>

Previous: POST Example | Back to Module | Next: Headers & Metadata