34918-vm/login.php
2025-10-13 09:51:27 +00:00

145 lines
4.3 KiB
PHP

<?php
session_start();
require_once 'db/config.php';
$error = '';
if (isset($_SESSION['user_id'])) {
header("location: index.php");
exit;
}
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$email = trim($_POST['email']);
$password = trim($_POST['password']);
if (empty($email) || empty($password)) {
$error = "Both email and password are required.";
} else {
try {
$pdo = db();
$sql = "SELECT * FROM users WHERE email = ?";
$stmt = $pdo->prepare($sql);
$stmt->execute([$email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if ($user && password_verify($password, $user['password'])) {
$_SESSION['user_id'] = $user['id'];
$_SESSION['user_name'] = $user['name'];
$_SESSION['user_role'] = $user['role'];
if ($user['role'] === 'admin') {
header("location: admin_dashboard.php"); // Create this page next
} else {
header("location: user_dashboard.php"); // Create this page next
}
exit;
} else {
$error = "The email or password you entered is incorrect.";
}
} catch (PDOException $e) {
$error = "Database error: " . $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 - READY BUDDY</title>
<link href="https://fonts.googleapis.com/css2?family=Poppins:wght@300;400;600;700&display=swap" rel="stylesheet">
<link rel="stylesheet" href="assets/css/style.css?v=<?php echo time(); ?>">
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f8f9fa;
}
.auth-container {
background: #fff;
padding: 2rem;
border-radius: 15px;
box-shadow: 0 10px 30px rgba(0,0,0,0.1);
width: 100%;
max-width: 400px;
text-align: center;
}
.auth-container h2 {
margin-bottom: 1.5rem;
font-weight: 700;
color: #333;
}
.form-group {
margin-bottom: 1rem;
text-align: left;
}
.form-group label {
display: block;
margin-bottom: 0.5rem;
font-weight: 600;
color: #555;
}
.form-control {
width: 100%;
padding: 0.75rem;
border: 1px solid #ddd;
border-radius: 8px;
font-size: 1rem;
}
.btn-submit {
background: linear-gradient(45deg, #0D6EFD, #0D6EFD);
color: #fff;
padding: 0.75rem;
border: none;
border-radius: 8px;
width: 100%;
font-size: 1rem;
font-weight: 600;
cursor: pointer;
transition: all 0.3s ease;
}
.btn-submit:hover {
opacity: 0.9;
}
.alert {
padding: 1rem;
margin-bottom: 1rem;
border-radius: 8px;
background-color: #f8d7da;
color: #721c24;
border: 1px solid #f5c6cb;
}
.auth-link {
margin-top: 1rem;
}
</style>
</head>
<body>
<div class="auth-container">
<h2>Login to Your Account</h2>
<?php if(!empty($error)): ?>
<div class="alert"><?php echo $error; ?></div>
<?php endif; ?>
<form action="/login" method="post">
<div class="form-group">
<label for="email">Email</label>
<input type="email" name="email" id="email" class="form-control" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input type="password" name="password" id="password" class="form-control" required>
</div>
<button type="submit" class="btn-submit">Login</button>
</form>
<p class="auth-link">Don't have an account? <a href="register.php">Register here</a></p>
</div>
</body>
</html>