35 lines
1.0 KiB
PHP
35 lines
1.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once __DIR__ . '/db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = $_POST['email'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
|
|
if (empty($email) || empty($password)) {
|
|
header('Location: index.php?error=Please fill in all fields');
|
|
exit;
|
|
}
|
|
|
|
try {
|
|
$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'];
|
|
$_SESSION['user_role'] = $user['role'];
|
|
header('Location: dashboard.php');
|
|
exit;
|
|
} else {
|
|
header('Location: index.php?error=Invalid email or password');
|
|
exit;
|
|
}
|
|
} catch (PDOException $e) {
|
|
// In a real app, log this error instead of showing it to the user
|
|
die('Database error: ' . $e->getMessage());
|
|
}
|
|
}
|