116 lines
5.0 KiB
PHP
116 lines
5.0 KiB
PHP
<?php
|
|
session_start();
|
|
require_once 'db/config.php';
|
|
|
|
// If user is already logged in, redirect to dashboard
|
|
if (isset($_SESSION['user_id'])) {
|
|
if ($_SESSION['role'] === 'guru') {
|
|
header("Location: dashboard_guru.php");
|
|
} else {
|
|
header("Location: dashboard_siswa.php");
|
|
}
|
|
exit();
|
|
}
|
|
|
|
$role = htmlspecialchars($_GET['role'] ?? 'Siswa');
|
|
$page_title = 'Login ' . ucfirst($role);
|
|
$error_message = '';
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = $_POST['username'] ?? '';
|
|
$password = $_POST['password'] ?? '';
|
|
$login_role = strtolower($role);
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error_message = "Username dan password tidak boleh kosong.";
|
|
} else {
|
|
try {
|
|
$stmt = db()->prepare("SELECT * FROM users WHERE username = ? AND role = ?");
|
|
$stmt->execute([$username, $login_role]);
|
|
$user = $stmt->fetch();
|
|
|
|
if ($user && password_verify($password, $user['password'])) {
|
|
// Password is correct, start session
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['role'] = $user['role'];
|
|
$_SESSION['full_name'] = $user['full_name'];
|
|
|
|
// Redirect to a dashboard page based on role
|
|
if ($user['role'] === 'guru') {
|
|
header("Location: dashboard_guru.php");
|
|
} else {
|
|
header("Location: dashboard_siswa.php");
|
|
}
|
|
exit();
|
|
} else {
|
|
// Use a generic error message to avoid user enumeration
|
|
$error_message = "Username atau password salah.";
|
|
}
|
|
} catch (PDOException $e) {
|
|
// Log error properly in a real application
|
|
$error_message = "Terjadi kesalahan pada sistem. Silakan coba lagi nanti.";
|
|
// error_log("Login failed: " . $e->getMessage());
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
|
<title><?php echo $page_title; ?> - Ulangan Harian Online</title>
|
|
<meta name="description" content="Halaman login untuk platform ulangan harian online SDN Sumberejo.">
|
|
<meta name="keywords" content="login, ujian online, sdn sumberejo, siswa, guru, admin, Built with Flatlogic Generator">
|
|
<meta property="og:title" content="<?php echo $page_title; ?> - Ulangan Harian Online">
|
|
<meta property="og:description" content="Halaman login untuk platform ulangan harian online SDN Sumberejo.">
|
|
<meta property="og:image" content="https://iili.io/KmIFoe1.md.png">
|
|
<meta name="twitter:card" content="summary_large_image">
|
|
<meta name="twitter:image" content="https://iili.io/KmIFoe1.md.png">
|
|
|
|
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet">
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.3/font/bootstrap-icons.min.css">
|
|
<link rel="stylesheet" href="assets/css/custom.css?v=<?php echo time(); ?>">
|
|
</head>
|
|
<body>
|
|
|
|
<div class="login-container">
|
|
<div class="col-lg-4 col-md-6 col-sm-10">
|
|
<div class="card login-card">
|
|
<div class="card-header">
|
|
<img src="https://iili.io/KmIFoe1.md.png" alt="Logo SDN Sumberejo">
|
|
<h3 class="mt-3"><?php echo $page_title; ?></h3>
|
|
</div>
|
|
<div class="card-body p-4">
|
|
<?php if (!empty($error_message)): ?>
|
|
<div class="alert alert-danger">
|
|
<?php echo $error_message; ?>
|
|
</div>
|
|
<?php endif; ?>
|
|
<form action="login.php?role=<?php echo strtolower($role); ?>" method="POST">
|
|
<div class="mb-3">
|
|
<label for="username" class="form-label">Username</label>
|
|
<input type="text" class="form-control" id="username" name="username" placeholder="Masukkan Username Anda" 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-gradient fw-bold">Login</button>
|
|
</div>
|
|
</form>
|
|
<div class="text-center mt-4">
|
|
<a href="index.php">Kembali ke Halaman Utama</a>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
|
|
<script src="assets/js/main.js?v=<?php echo time(); ?>"></script>
|
|
</body>
|
|
</html>
|