91 lines
3.5 KiB
PHP
91 lines
3.5 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
// Check for success message from registration
|
|
if (isset($_SESSION['success_message'])) {
|
|
$success = $_SESSION['success_message'];
|
|
unset($_SESSION['success_message']);
|
|
}
|
|
|
|
if ($_SERVER["REQUEST_METHOD"] == "POST") {
|
|
$username = trim($_POST['username']);
|
|
$password = trim($_POST['password']);
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = "Please enter both username and password.";
|
|
} else {
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT users.id, users.username, users.password_hash, roles.role_name FROM users JOIN roles ON users.role_id = roles.id WHERE users.username = ?");
|
|
$stmt->execute([$username]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user && password_verify($password, $user['password_hash'])) {
|
|
// Password is correct, start a new session
|
|
session_regenerate_id();
|
|
$_SESSION['loggedin'] = true;
|
|
$_SESSION['id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['role'] = $user['role_name'];
|
|
|
|
header("location: index.php");
|
|
exit;
|
|
} else {
|
|
// Display an error message if password or username is not valid
|
|
$error = "The username or password you entered was not valid.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="en">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title>Login - Bhuddi School</title>
|
|
<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">
|
|
</head>
|
|
<body>
|
|
<div class="container mt-5">
|
|
<div class="row justify-content-center">
|
|
<div class="col-md-6">
|
|
<div class="card">
|
|
<div class="card-header">
|
|
Login
|
|
</div>
|
|
<div class="card-body">
|
|
<?php if(!empty($success)): ?>
|
|
<div class="alert alert-success" role="alert">
|
|
<?php echo $success; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<?php if(!empty($error)): ?>
|
|
<div class="alert alert-danger" role="alert">
|
|
<?php echo $error; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form action="login.php" method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" 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>
|
|
<div class="card-footer text-center">
|
|
Don't have an account? <a href="signup.php">Sign up here</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|