12) { $errors['username'] = 'Username must be between 3 and 12 characters long.'; } elseif (!preg_match('/^[a-z0-9]+$/', $username)) { $errors['username'] = 'Username can only contain lowercase letters (a-z) and numbers (0-9).'; } else { $stmt = $pdo->prepare('SELECT 1 FROM users WHERE username = :username'); $stmt->execute(['username' => $username]); if ($stmt->fetch()) { $errors['username'] = 'That username is not available.'; } } if ($email === '') { $errors['email'] = 'Email is required.'; } elseif (strlen($email) > 100) { $errors['email'] = 'Email cannot exceed 100 characters.'; } elseif (!filter_var($email, FILTER_VALIDATE_EMAIL)) { $errors['email'] = 'Please enter a valid email address.'; } else { $stmt = $pdo->prepare('SELECT 1 FROM users WHERE email = :email'); $stmt->execute(['email' => $email]); if ($stmt->fetch()) { $errors['email'] = "We couldn't create your account with those details. If you already have an account, try logging in instead."; } } if ($password === '') { $errors['password'] = 'Password is required.'; } elseif (strlen($password) < 8) { $errors['password'] = 'Password must be at least 8 characters long.'; } elseif (strlen($password) > 72) { $errors['password'] = 'Password cannot exceed 72 characters.'; } if (empty($errors)) { $hashedPassword = password_hash($password, PASSWORD_DEFAULT); try { $stmt = $pdo->prepare('INSERT INTO users (username, email, password, credits) VALUES (:username, :email, :password, :credits)'); $stmt->execute([ 'username' => $username, 'email' => $email, 'password' => $hashedPassword, 'credits' => STARTING_CREDITS, ]); } catch (PDOException $e) { if (str_contains($e->getMessage(), 'UNIQUE constraint failed: users.username')) { $errors['username'] = 'That username is not available.'; } elseif (str_contains($e->getMessage(), 'UNIQUE constraint failed: users.email')) { $errors['email'] = "We couldn't create your account with those details. If you already have an account, try logging in instead."; } else { throw $e; } } if (empty($errors)) { start_app_session(); session_regenerate_id(true); $_SESSION['user_id'] = (int) $pdo->lastInsertId(); $_SESSION['username'] = $username; sendRegistrationConfirmationEmail($email, $username); header('Location: /'); exit; } } } $pageTitle = 'Register'; ?>

Start your collection.

Create an account and bring your imagination to life.

Already have an account? Log in