37332-vm/login.php
Flatlogic Bot 298521e30e bb
2026-01-09 01:55:06 +00:00

112 lines
4.3 KiB
PHP

<?php
// If user is already logged in, redirect to home page
if (isset($_SESSION['user_id'])) {
header("Location: index.php");
exit();
}
require_once 'db/config.php';
$email = '';
$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)) {
try {
$pdo = db();
$stmt = $pdo->prepare("SELECT id, name, email, password, role FROM users WHERE email = ?");
$stmt->execute([$email]);
$user = $stmt->fetch();
if ($user && password_verify($password, $user['password'])) {
// Password is correct, start session
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name'];
$_SESSION['user_role'] = $user['role'];
// Redirect to home page
header("Location: index.php");
exit();
} else {
$errors[] = 'Invalid email or password.';
}
} catch (PDOException $e) {
$errors[] = "Database error: Could not log in.";
// In a real app, you would log this error.
// error_log("Login failed: " . $e->getMessage());
}
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login - Event Platform</title>
<link href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700&display=swap" rel="stylesheet">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
</head>
<body class="bg-light">
<?php require_once './includes/header.php'; ?>
<main class="container mt-5 pt-5">
<div class="row justify-content-center">
<div class="col-md-6 col-lg-5">
<div class="card border-0 shadow-lg">
<div class="card-body p-4 p-md-5">
<h2 class="card-title text-center mb-4" style="font-weight: 700;">Login to Your Account</h2>
<?php if (!empty($errors)): ?>
<div class="alert alert-danger" role="alert">
<?php foreach ($errors as $error): ?>
<p class="mb-0"><?php echo htmlspecialchars($error); ?></p>
<?php endforeach; ?>
</div>
<?php endif; ?>
<form action="login.php" method="POST" novalidate>
<div class="mb-3">
<label for="email" class="form-label">Email address</label>
<input type="email" class="form-control" id="email" name="email" value="<?php echo htmlspecialchars($email); ?>" required>
</div>
<div class="mb-4">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<div class="d-grid">
<button type="submit" class="btn btn-primary btn-lg" style="background-color: #4F46E5;">Login</button>
</div>
</form>
<p class="text-center mt-4">
Don't have an account? <a href="register.php">Sign up</a>
</p>
</div>
</div>
</div>
</div>
</main>
<footer class="text-center py-4 text-muted fixed-bottom bg-light">
<p>&copy; <?php echo date("Y"); ?> EventPlatform. All rights reserved.</p>
</footer>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>