36318-vm/handle_login.php
Flatlogic Bot 5274c73966 Base app
2025-11-26 13:53:30 +00:00

33 lines
1.0 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
require_once 'includes/flash_messages.php';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
$email = $_POST['email'];
$password = $_POST['password'];
try {
$pdo = db();
$stmt = $pdo->prepare('SELECT id, 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'];
set_flash_message('You have been successfully logged in.', 'success');
header("Location: index.php");
exit;
} else {
set_flash_message('Invalid email or password.', 'danger');
header("Location: login.php");
exit;
}
} catch (PDOException $e) {
set_flash_message('Database error. Please try again.', 'danger');
error_log("Login DB Error: " . $e->getMessage());
header("Location: login.php");
exit;
}
}