88 lines
3.0 KiB
PHP
88 lines
3.0 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
include 'db.php';
|
|
|
|
session_start();
|
|
|
|
$err = '';
|
|
$username_input = '';
|
|
|
|
if (isset($_POST['login'])) {
|
|
$username_input = trim($_POST['username'] ?? '');
|
|
$pass = $_POST['password'] ?? '';
|
|
|
|
if ($username_input === '' || $pass === '') {
|
|
$err = "Username dan password harus diisi.";
|
|
} else {
|
|
try {
|
|
// 1. Ambil data user berdasarkan username
|
|
// Menggunakan Prepared Statement
|
|
$stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ?");
|
|
$stmt->execute([$username_input]);
|
|
$user_data = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
// 2. Verifikasi Password
|
|
// password_verify akan mencocokkan input user dengan HASH di database
|
|
if ($user_data && password_verify($pass, $user_data['password'])) {
|
|
|
|
// Regenerasi ID Session
|
|
session_regenerate_id(true);
|
|
|
|
// Simpan data ke session
|
|
$_SESSION['user_id'] = $user_data['id'];
|
|
$_SESSION['username'] = $user_data['username'];
|
|
$_SESSION['login'] = true;
|
|
|
|
// Arahkan ke halaman game
|
|
header("Location: sudoku.php");
|
|
exit();
|
|
|
|
} else {
|
|
$err = "Username atau password salah.";
|
|
}
|
|
|
|
} catch (PDOException $e) {
|
|
$err = "Terjadi kesalahan sistem database.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<html>
|
|
<head>
|
|
<title>Login</title>
|
|
<style>
|
|
/* Style disamakan persis dengan register.php Anda */
|
|
body { font-family: Arial; background:#eef2f7; display:flex; height:100vh; justify-content:center; align-items:center; margin:0; }
|
|
.card { width:350px; background:white; padding:20px; border-radius:10px; box-shadow:0 6px 20px rgba(0,0,0,0.1); }
|
|
.input { width:100%; padding:10px; margin:8px 0; border:1px solid #ccc; border-radius:8px; box-sizing: border-box; }
|
|
.btn { width:100%; padding:12px; background:#007bff; color:white; border:none; border-radius:8px; cursor:pointer; }
|
|
.btn:hover { background:#0056b3; }
|
|
.err { color:#d00000; margin-bottom:10px; text-align:center; background: #ffe6e6; padding: 5px; border-radius: 5px;}
|
|
.link { text-align:center; margin-top:10px; }
|
|
a { text-decoration: none; color: #007bff; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
|
|
<div class="card">
|
|
<h2 style="text-align: center">Login</h2>
|
|
|
|
<?php if ($err): ?>
|
|
<div class="err"><?= htmlspecialchars($err) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST" action="login.php">
|
|
<input class="input" type="text" name="username" placeholder="Masukkan Username" value="<?= htmlspecialchars($username_input) ?>" required>
|
|
<input class="input" type="password" name="password" placeholder="Masukkan Password" required>
|
|
<button class="btn" type="submit" name="login">Masuk</button>
|
|
</form>
|
|
|
|
<div class="link">
|
|
Belum punya akun? <a href="register.php">Daftar</a>
|
|
</div>
|
|
</div>
|
|
|
|
</body>
|
|
</html>
|