Memperbaiki register

This commit is contained in:
aldo 2025-12-01 14:50:26 +07:00
parent 6fc5352be1
commit f3ac99887d
2 changed files with 66 additions and 58 deletions

View File

@ -2,7 +2,7 @@
ini_set('display_errors', 1); ini_set('display_errors', 1);
error_reporting(E_ALL); error_reporting(E_ALL);
include 'db.php'; // Pastikan file koneksi PDO Anda benar include 'db.php';
session_start(); session_start();
@ -18,18 +18,18 @@ if (isset($_POST['login'])) {
} else { } else {
try { try {
// 1. Ambil data user berdasarkan username // 1. Ambil data user berdasarkan username
// Menggunakan Prepared Statement (Aman dari SQL Injection) // Menggunakan Prepared Statement
$stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ?"); $stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ?");
$stmt->execute([$username_input]); $stmt->execute([$username_input]);
$user_data = $stmt->fetch(PDO::FETCH_ASSOC); $user_data = $stmt->fetch(PDO::FETCH_ASSOC);
// 2. Verifikasi Password // 2. Verifikasi Password
// password_verify akan mencocokkan input user dengan HASH di database (seperti milik 'bejo') // password_verify akan mencocokkan input user dengan HASH di database
if ($user_data && password_verify($pass, $user_data['password'])) { if ($user_data && password_verify($pass, $user_data['password'])) {
// Login Berhasil! // Login Berhasil!
// Regenerasi ID Session (Security Best Practice) // Regenerasi ID Session
session_regenerate_id(true); session_regenerate_id(true);
// Simpan data ke session // Simpan data ke session
@ -37,7 +37,7 @@ if (isset($_POST['login'])) {
$_SESSION['username'] = $user_data['username']; $_SESSION['username'] = $user_data['username'];
$_SESSION['login'] = true; $_SESSION['login'] = true;
// Arahkan ke halaman game (sesuai gambar pertama Anda) // Arahkan ke halaman game
header("Location: sudoku.php"); header("Location: sudoku.php");
exit(); exit();
@ -51,14 +51,10 @@ if (isset($_POST['login'])) {
} }
} }
?> ?>
<!DOCTYPE html> <html>
<html lang="id">
<head> <head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login</title> <title>Login</title>
<style> <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; } 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); } .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; } .input { width:100%; padding:10px; margin:8px 0; border:1px solid #ccc; border-radius:8px; box-sizing: border-box; }

View File

@ -2,83 +2,95 @@
ini_set('display_errors', 1); ini_set('display_errors', 1);
error_reporting(E_ALL); error_reporting(E_ALL);
include 'db.php'; // Pastikan $conn ada di sini include 'db.php';
session_start(); session_start();
$err = ''; // Variabel untuk pesan error $err = '';
$ok = ''; // Variabel untuk pesan sukses $success = '';
$username_input = '';
// Regex password: minimal 6, huruf + angka
function password_valid($p) {
return preg_match('/^(?=.*[A-Za-z])(?=.*\d).{6,}$/', $p);
}
if (isset($_POST['register'])) { if (isset($_POST['register'])) {
// 1. Ambil dan bersihkan input $username_input = trim($_POST['username'] ?? '');
$user = trim($_POST['username'] ?? '');
$pass = $_POST['password'] ?? ''; $pass = $_POST['password'] ?? '';
$pass2 = $_POST['password_confirm'] ?? ''; $konfirmasi_pass = $_POST['konfirmasi_password'] ?? '';
// 2. Validasi Input if ($username_input === '' || $pass === '' || $konfirmasi_pass === '') {
if ($user === '' || $pass === '' || $pass2 === '') { $err = "Semua kolom wajib diisi.";
$err = "Semua field harus diisi."; }
} elseif ($pass !== $pass2) { elseif ($pass !== $konfirmasi_pass) {
$err = "Konfirmasi password tidak cocok."; $err = "Konfirmasi password tidak cocok.";
} elseif (!password_valid($pass)) { }
$err = "Password minimal 6 karakter, harus mengandung huruf & angka."; elseif (strlen($pass) < 6) {
} else { $err = "Password terlalu pendek. Minimal 6 karakter.";
}
else {
try { try {
// 3. Hash Password (Keamanan Kritis!) $check = $conn->prepare("SELECT id FROM users WHERE username = ?");
$hash = password_hash($pass, PASSWORD_DEFAULT); $check->execute([$username_input]);
// 4. Prepared Statement (Mencegah SQL Injection) if($check->rowCount() > 0){
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)"); $err = "Username sudah terdaftar, silakan pilih nama lain.";
$stmt->execute([$user, $hash]); // Eksekusi query dengan data
$ok = "Registrasi berhasil, silakan login.";
} catch (PDOException $e) {
// Tangani error jika username sudah ada (Unique Constraint)
if ($e->getCode() == "23000") {
$err = "Username sudah digunakan.";
} else { } else {
$err = "Error: Terjadi kesalahan saat registrasi database."; $hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
$stmt->execute([$username_input, $hashed_pass]);
$success = "Registrasi berhasil! Silakan <a href='login.php'>Login disini</a>.";
$username_input = '';
} }
} catch (PDOException $e) {
$err = "Terjadi kesalahan sistem: " . $e->getMessage();
} }
} }
} }
?> ?>
<!DOCTYPE html> <html>
<html lang="id">
<head> <head>
<meta charset="UTF-8"> <title>Daftar Akun Baru</title>
<title>Register</title>
<style> <style>
body { font-family: Arial; background:#eef2f7; display:flex; height:100vh; justify-content:center; align-items:center; margin:0; } 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); } .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; } .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:#28a745; color:white; border:none; border-radius:8px; cursor:pointer; } .btn { width:100%; padding:12px; background:#28a745; color:white; border:none; border-radius:8px; cursor:pointer; font-weight: bold;}
.err { color:#d00000; margin-bottom:10px; text-align:center; } .btn:hover { background:#218838; }
.ok { color:green; margin-bottom:10px; text-align:center; } .err { color:#721c24; margin-bottom:10px; text-align:center; background: #f8d7da; padding: 10px; border-radius: 5px; border: 1px solid #f5c6cb; font-size: 14px;}
.link { text-align:center; margin-top:10px; } .success { color:#155724; margin-bottom:10px; text-align:center; background: #d4edda; padding: 10px; border-radius: 5px; border: 1px solid #c3e6cb;}
.link { text-align:center; margin-top:10px; font-size: 14px; color: #666; }
a { text-decoration: none; color: #007bff; }
h2 { text-align: center; margin-top: 0; color: #333; }
p { text-align:center; color:gray; font-size: 14px; margin-bottom: 20px; }
</style> </style>
</head> </head>
<body> <body>
<div class="card"> <div class="card">
<h2>Register</h2> <h2>Register</h2>
<?php if ($err): ?>
<div class="err"><?= htmlspecialchars($err) ?></div>
<?php endif; ?>
<?php if ($success): ?>
<div class="success"><?= $success ?></div>
<?php endif; ?>
<form method="POST" action="register.php"> <form method="POST" action="">
<input class="input" type="text" name="username" placeholder="Masukkan Username"> <input class="input" type="text" name="username" placeholder="Username" value="<?= htmlspecialchars($username_input) ?>" required autocomplete="off">
<input class="input" type="password" name="password" placeholder="Masukkan Password">
<input class="input" type="password" name="password_confirm" placeholder="Konfirmasi Password"> <input class="input" type="password" name="password" placeholder="Password (Min 6 huruf)" required minlength="6">
<button class="btn" type="submit" name="register">Daftar</button>
<input class="input" type="password" name="konfirmasi_password" placeholder="Ulangi Password" required>
<button type="submit" name="register" class="btn">Daftar Sekarang</button>
</form> </form>
<div class="link"> <div class="link">
Sudah punya akun? <a href="login.php">Login</a> Sudah punya akun? <a href="login.php">Login disini</a>
</div> </div>
</div> </div>
</body> </body>
</html> </html>