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>
77 lines
2.2 KiB
PHP
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>
|