34581-vm/login.php
Flatlogic Bot 4ed2abbf31 1.0
2025-10-02 02:55:03 +00:00

77 lines
2.8 KiB
PHP

<?php
require_once __DIR__ . '/includes/common.php';
require_guest();
$title = 'Login';
$errors = [];
$username = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$username = trim($_POST['username'] ?? '');
$password = $_POST['password'] ?? '';
if (empty($username) || empty($password)) {
$errors[] = 'Username and password are required.';
} else {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT id, username, password_hash FROM users WHERE username = ?");
$stmt->execute([$username]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password_hash'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header('Location: index.php');
exit;
} else {
$errors[] = 'Invalid username or password.';
}
} catch (PDOException $e) {
$errors[] = 'Database error. Please try again later.';
}
}
}
require_once __DIR__ . '/includes/header.php';
?>
<div class="row justify-content-center">
<div class="col-lg-5 col-md-7">
<div class="card">
<div class="card-body p-5">
<h1 class="card-title text-center mb-4">Login</h1>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<p class="mb-0"><?= htmlspecialchars($errors[0]) ?></p>
</div>
<?php endif; ?>
<form action="login.php" method="POST" class="needs-validation" novalidate>
<div class="form-floating mb-3">
<input type="text" class="form-control" id="username" name="username" placeholder="Username" value="<?= htmlspecialchars($username) ?>" required>
<label for="username">Username</label>
<div class="invalid-feedback">Please enter your username.</div>
</div>
<div class="form-floating mb-3">
<input type="password" class="form-control" id="password" name="password" placeholder="Password" required>
<label for="password">Password</label>
<div class="invalid-feedback">Please enter your password.</div>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary btn-lg">Login</button>
</div>
</form>
<p class="text-center mt-4">
Don't have an account? <a href="register.php">Register here</a>.
</p>
</div>
</div>
</div>
</div>
<?php
require_once __DIR__ . '/includes/footer.php';
?>