You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
FutureCraft/login.php

66 lines
2.1 KiB

<?php
require 'db_connection.php';
session_start();
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
// Get user inputs
$email = $_POST['email'];
$password = $_POST['password'];
// Query the database to fetch user details
$stmt = $conn->prepare("SELECT * FROM users WHERE email = ?");
$stmt->bind_param("s", $email);
$stmt->execute();
$result = $stmt->get_result();
$user = $result->fetch_assoc();
// Check if user exists and password is correct
if ($user && password_verify($password, $user['password'])) {
// Store user data in session
$_SESSION['user_email'] = $user['email'];
$_SESSION['role'] = $user['role'];
$_SESSION['user_id'] = $user['id'];
// Redirect to user dashboard or admin dashboard based on role
if ($user['role'] === 'admin') {
header("Location: admin_dashboard.php");
} else {
header("Location: dashboard.php");
}
exit;
} else {
$error_message = "Invalid email or password.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css">
</head>
<body>
<div class="container mt-5">
<h2>Login</h2>
<?php if (isset($error_message)): ?>
<div class="alert alert-danger"><?= $error_message ?></div>
<?php endif; ?>
<form method="POST">
<div class="mb-3">
<label for="email" class="form-label">Email</label>
<input type="email" class="form-control" id="email" name="email" required>
</div>
<div class="mb-3">
<label for="password" class="form-label">Password</label>
<input type="password" class="form-control" id="password" name="password" required>
</div>
<button type="submit" class="btn btn-primary">Login</button>
</form>
</div>
</body>
</html>