Files
Critters/login.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

77 lines
2.2 KiB
PHP

<?php
require __DIR__ . '/session.php';
require __DIR__ . '/db.php';
start_app_session();
if (isset($_SESSION['username'])) {
header('Location: /');
exit;
}
$errors = [];
$username = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
sleep(4);
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if ($username === '') {
$errors['username'] = 'Username is required.';
}
if ($password === '') {
$errors['password'] = 'Password is required.';
}
if (empty($errors)) {
$pdo = getDbConnection();
$stmt = $pdo->prepare('SELECT * FROM users WHERE username = :username');
$stmt->execute(['username' => $username]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user || !password_verify($password, $user['password'])) {
$errors['login'] = 'Invalid username or password.';
} else {
start_app_session();
session_regenerate_id(true);
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header('Location: /');
exit;
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Log In</title>
</head>
<body>
<?php if (isset($errors['login'])): ?>
<div><?php echo htmlspecialchars($errors['login'], ENT_QUOTES, 'UTF-8'); ?></div>
<?php endif; ?>
<form action="login.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="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">Log In</button>
</form>
</body>
</html>