58 lines
2.1 KiB
PHP
58 lines
2.1 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error_message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$email = trim($_POST['email']);
|
|
$password = $_POST['password'];
|
|
|
|
if (empty($email) || empty($password)) {
|
|
$error_message = 'Please fill in all fields.';
|
|
} else {
|
|
try {
|
|
$db = db();
|
|
$stmt = $db->prepare("SELECT id, username, password FROM users WHERE email = :email");
|
|
$stmt->bindParam(':email', $email);
|
|
$stmt->execute();
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username']; // Store username for display
|
|
header('Location: home.php'); // Redirect to home.php
|
|
exit;
|
|
} else {
|
|
$error_message = 'Invalid email or password.';
|
|
}
|
|
} catch (PDOException $e) {
|
|
$error_message = 'Database error: ' . $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
$pageTitle = "Login - Flatlogic";
|
|
include 'includes/header.php';
|
|
?>
|
|
<div class="container">
|
|
<header>
|
|
<h1>Login</h1>
|
|
</header>
|
|
<main>
|
|
<?php if ($error_message): ?>
|
|
<div class="message error"><?php echo $error_message; ?></div>
|
|
<?php endif; ?>
|
|
<form action="login.php" method="post" class="form-card">
|
|
<div class="form-group">
|
|
<label for="email">Email</label>
|
|
<input type="email" id="email" name="email" required>
|
|
</div>
|
|
<div class="form-group">
|
|
<label for="password">Password</label>
|
|
<input type="password" id="password" name="password" required>
|
|
</div>
|
|
<button type="submit" class="button">Login</button>
|
|
</form>
|
|
<p class="text-center">Don't have an account? <a href="register.php">Register here</a>.</p>
|
|
</main>
|
|
<?php include 'includes/footer.php'; ?>
|