79 lines
2.2 KiB
PHP
79 lines
2.2 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$errors = [];
|
|
|
|
if (isset($_SESSION['user_id'])) {
|
|
header('Location: index.php');
|
|
exit;
|
|
}
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
$errors[] = 'A valid email is required.';
|
|
}
|
|
|
|
if (empty($password)) {
|
|
$errors[] = 'Password is required.';
|
|
}
|
|
|
|
if (empty($errors)) {
|
|
try {
|
|
$pdo = db();
|
|
|
|
$stmt = $pdo->prepare("SELECT id, password FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
header('Location: index.php');
|
|
exit;
|
|
} else {
|
|
$errors[] = 'Invalid email or password.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$errors[] = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
include 'header.php';
|
|
?>
|
|
|
|
<div class="auth-container">
|
|
<div class="auth-form">
|
|
<h2>Login</h2>
|
|
|
|
<?php if (!empty($errors)): ?>
|
|
<div class="errors">
|
|
<?php foreach ($errors as $error): ?>
|
|
<p><?php echo htmlspecialchars($error); ?></p>
|
|
<?php endforeach; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
|
|
<form action="login.php" method="post">
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" name="email" id="email" required placeholder="you@example.com">
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" name="password" id="password" required placeholder="••••••••">
|
|
</div>
|
|
<button type="submit" class="btn-submit">Login</button>
|
|
</form>
|
|
|
|
<p class="auth-link">Don't have an account? <a href="register.php">Register here</a>.</p>
|
|
</div>
|
|
</div>
|
|
|
|
<?php
|
|
include 'footer.php';
|
|
?>
|