41 lines
1.1 KiB
PHP
41 lines
1.1 KiB
PHP
<?php
|
|
require_once __DIR__ . '/db/config.php';
|
|
require_once __DIR__ . '/includes/session.php';
|
|
|
|
$errors = [];
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = trim($_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)) {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE email = ?");
|
|
$stmt->execute([$email]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['user_name'] = $user['name'];
|
|
header('Location: /dashboard.php');
|
|
exit;
|
|
} else {
|
|
$errors[] = 'Invalid email or password.';
|
|
}
|
|
}
|
|
$_SESSION['login_errors'] = $errors;
|
|
header('Location: /index.php#loginModal');
|
|
exit;
|
|
}
|
|
|
|
// This script does not render HTML. It only processes the form.
|
|
header('Location: /index.php');
|
|
exit;
|