37 lines
1.0 KiB
PHP
37 lines
1.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
|
|
$username = $_POST['username'];
|
|
$password = $_POST['password'];
|
|
|
|
$pdo = db();
|
|
$stmt = $pdo->prepare("SELECT id, password, role FROM users WHERE username = ?");
|
|
$stmt->execute([$username]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['role'] = $user['role'];
|
|
header('Location: ' . ($user['role'] == 'admin' ? 'admin/dashboard.php' : 'member/dashboard.php'));
|
|
exit;
|
|
} else {
|
|
$error = "Invalid username or password.";
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head><title>Login</title></head>
|
|
<body>
|
|
<h1>Login</h1>
|
|
<?php if (isset($error)) echo "<p>$error</p>"; ?>
|
|
<form method="POST">
|
|
Username: <input type="text" name="username" required><br>
|
|
Password: <input type="password" name="password" required><br>
|
|
<button type="submit">Login</button>
|
|
</form>
|
|
</body>
|
|
</html>
|