33966-vm/login.php
Flatlogic Bot e62cdf478f v1
2025-09-10 17:18:29 +00:00

86 lines
3.0 KiB
PHP

<?php
require_once 'db/config.php';
require_once 'templates/header.php';
$errors = [];
$email = '';
if (isset($_SESSION['user_id'])) {
header("Location: admin.php");
exit();
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = trim($_POST['email']);
$password = $_POST['password'];
if (empty($email)) {
$errors[] = 'Email is required';
}
if (empty($password)) {
$errors[] = 'Password is required';
}
if (empty($errors)) {
$stmt = db()->prepare("SELECT id, username, password_hash FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password_hash'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['username'] = $user['username'];
header("Location: admin.php");
exit();
} else {
$errors[] = 'Invalid email or password';
}
}
}
?>
<main class="container my-5">
<section class="py-5">
<div class="container">
<div class="row justify-content-center">
<div class="col-lg-6">
<div class="feature-card p-4">
<h2 class="text-center mb-4">Login</h2>
<?php if (isset($_GET['registration']) && $_GET['registration'] === 'success'): ?>
<div class="alert alert-success">
Registration successful! You can now log in.
</div>
<?php endif; ?>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger">
<?php foreach ($errors as $error): ?>
<p class="m-0"><?php echo $error; ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form method="POST" action="login.php">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary">Login</button>
</div>
</form>
<p class="text-center mt-3">
Don't have an account? <a href="register.php">Register here</a>.
</p>
</div>
</div>
</div>
</div>
</section>
</main>
<?php require_once 'templates/footer.php'; ?>