Files
Critters/register.php
T
pauljdandClaude Sonnet 5 aab614f811
pauljd/davidson-ci: Davidson CI / quality (push) Successful in 13s
pauljd/davidson-ci: Davidson CI / ai-review (push) Successful in 1m2s
Add auth, critter creation with AI image generation, and Tailwind styling
Adds login/logout/session handling, a create-critter form backed by
adjectives/nouns/verbs/locations lookup tables, Replicate-generated
critter images saved to disk, and a Tailwind-styled critter gallery
on the homepage.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-09-05 22:41:20 +01:00

113 lines
3.9 KiB
PHP

<?php
require __DIR__ . '/session.php';
require __DIR__ . '/db.php';
start_app_session();
if (isset($_SESSION['username'])) {
header('Location: /');
exit;
}
$errors = [];
$username = '';
$email = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$email = trim($_POST['email'] ?? '');
$password = $_POST['password'] ?? '';
$email = filter_var($email, FILTER_SANITIZE_EMAIL);
$pdo = getDbConnection();
if ($username === '') {
$errors['username'] = 'Username is required.';
} elseif (strlen($username) < 3 || strlen($username) > 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'] = 'That email address is already registered.';
}
}
if ($password === '') {
$errors['password'] = 'Password is required.';
} elseif (strlen($password) > 100) {
$errors['password'] = 'Password cannot exceed 100 characters.';
} elseif (strlen($password) < 6 || strlen($password) > 12) {
$errors['password'] = 'Password must be between 6 and 12 characters long.';
}
if (empty($errors)) {
$hashedPassword = password_hash($password, PASSWORD_DEFAULT);
$stmt = $pdo->prepare('INSERT INTO users (username, email, password) VALUES (:username, :email, :password)');
$stmt->execute([
'username' => $username,
'email' => $email,
'password' => $hashedPassword,
]);
start_app_session();
session_regenerate_id(true);
$_SESSION['user_id'] = (int) $pdo->lastInsertId();
$_SESSION['username'] = $username;
header('Location: /');
exit;
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Register</title>
</head>
<body>
<form action="register.php" method="post" novalidate>
<div>
<label for="username">Username</label>
<input type="text" id="username" name="username" value="<?php echo htmlspecialchars($username, ENT_QUOTES, 'UTF-8'); ?>">
<?php if (isset($errors['username'])): ?>
<div><?php echo htmlspecialchars($errors['username'], ENT_QUOTES, 'UTF-8'); ?></div>
<?php endif; ?>
</div>
<div>
<label for="email">Email</label>
<input type="text" id="email" name="email" value="<?php echo htmlspecialchars($email, ENT_QUOTES, 'UTF-8'); ?>">
<?php if (isset($errors['email'])): ?>
<div><?php echo htmlspecialchars($errors['email'], ENT_QUOTES, 'UTF-8'); ?></div>
<?php endif; ?>
</div>
<div>
<label for="password">Password</label>
<input type="password" id="password" name="password">
<?php if (isset($errors['password'])): ?>
<div><?php echo htmlspecialchars($errors['password'], ENT_QUOTES, 'UTF-8'); ?></div>
<?php endif; ?>
</div>
<button type="submit">Sign Up</button>
</form>
</body>
</html>