// Validation if (empty($username)) $errors['username'] = "Username is required"; if (empty($email)) $errors['email'] = "Email is required"; if (!filter_var($email, FILTER_VALIDATE_EMAIL)) $errors['email'] = "Invalid email format"; if (strlen($password) < 6) $errors['password'] = "Password must be at least 6 characters"; if ($password !== $confirm_password) $errors['confirm_password'] = "Passwords do not match";

function sanitizeInput($data) { return htmlspecialchars(strip_tags(trim($data))); }

try { $stmt = $pdo->prepare("INSERT INTO users (username, email, password_hash) VALUES (?, ?, ?)"); $stmt->execute([$username, $email, $password_hash]); redirect('login.php?registered=1'); } catch(PDOException $e) { if ($e->errorInfo[1] == 1062) { $errors['database'] = "Username or email already exists"; } else { $errors['database'] = "Registration failed. Please try again."; } } } } ?> <?php include 'includes/header.php'; ?> <h2>Register</h2> <?php if (isset($errors['database'])): ?> <div class="error"><?= $errors['database'] ?></div> <?php endif; ?> <form method="POST"> <div> <label>Username:</label> <input type="text" name="username" value="<?= htmlspecialchars($username ?? '') ?>"> <?php displayError($errors, 'username'); ?> </div> <div> <label>Email:</label> <input type="email" name="email" value="<?= htmlspecialchars($email ?? '') ?>"> <?php displayError($errors, 'email'); ?> </div> <div> <label>Password (min 6 chars):</label> <input type="password" name="password"> <?php displayError($errors, 'password'); ?> </div> <div> <label>Confirm Password:</label> <input type="password" name="confirm_password"> <?php displayError($errors, 'confirm_password'); ?> </div> <button type="submit">Register</button> </form> <?php include 'includes/footer.php'; ?> <?php require_once 'config/database.php'; require_once 'includes/functions.php'; if (isLoggedIn()) { redirect('dashboard.php'); }

// Fetch user's items $stmt = $pdo->prepare("SELECT * FROM items WHERE user_id = ? ORDER BY created_at DESC"); $stmt->execute([$_SESSION['user_id']]); $items = $stmt->fetchAll(); ?> <?php include 'includes/header.php'; ?> <h2>Welcome, <?= htmlspecialchars($_SESSION['username']) ?>!</h2>

Copy the code, run the SQL script, and you'll have a working user system in minutes. Happy coding! Need help? Leave a comment or check the PHP manual for more details on PDO and sessions.

* { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; line-height: 1.6; background: #f4f4f4; }