75 lines
2.2 KiB
PHP
75 lines
2.2 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
session_start();
|
|
|
|
$dbFile = __DIR__ . '/users.sqlite';
|
|
$redirectAfterLogin = 'sudoku.php';
|
|
|
|
try {
|
|
$pdo = new PDO('sqlite:' . $dbFile);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch (Exception $e) {
|
|
die("DB Error: " . $e->getMessage());
|
|
}
|
|
|
|
$err = "";
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$user = trim($_POST['username'] ?? '');
|
|
$pass = $_POST['password'] ?? '';
|
|
|
|
if ($user === '' || $pass === '') {
|
|
$err = "Isi username dan password.";
|
|
} else {
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = :u LIMIT 1");
|
|
$stmt->execute([':u' => $user]);
|
|
$row = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($row && password_verify($pass, $row['password'])) {
|
|
session_regenerate_id(true);
|
|
$_SESSION['user'] = $row['username'];
|
|
header("Location: $redirectAfterLogin");
|
|
exit;
|
|
} else {
|
|
$err = "Username atau password salah.";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Login</title>
|
|
<style>
|
|
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; }
|
|
.btn { width:100%; padding:12px; background:#1e90ff; color:white; border:none; border-radius:8px; cursor:pointer; }
|
|
.err { color:#d00000; margin-bottom:10px; text-align:center; }
|
|
.link { text-align:center; margin-top:10px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="card">
|
|
<h2>Login</h2>
|
|
|
|
<?php if ($err): ?>
|
|
<div class="err"><?= htmlspecialchars($err) ?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="post">
|
|
<input class="input" type="text" name="username" placeholder="Username">
|
|
<input class="input" type="password" name="password" placeholder="Password">
|
|
<button class="btn" type="submit">Masuk</button>
|
|
</form>
|
|
|
|
<div class="link">
|
|
Belum punya akun? <a href="register.php">Daftar</a>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|