85 lines
2.9 KiB
PHP
85 lines
2.9 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
error_reporting(E_ALL);
|
|
|
|
include 'db.php'; // Pastikan $conn ada di sini
|
|
|
|
session_start();
|
|
|
|
$err = ''; // Variabel untuk pesan error
|
|
$ok = ''; // Variabel untuk pesan sukses
|
|
|
|
// Regex password: minimal 6, huruf + angka
|
|
function password_valid($p) {
|
|
return preg_match('/^(?=.*[A-Za-z])(?=.*\d).{6,}$/', $p);
|
|
}
|
|
|
|
if (isset($_POST['register'])) {
|
|
// 1. Ambil dan bersihkan input
|
|
$user = trim($_POST['username'] ?? '');
|
|
$pass = $_POST['password'] ?? '';
|
|
$pass2 = $_POST['password_confirm'] ?? '';
|
|
|
|
// 2. Validasi Input
|
|
if ($user === '' || $pass === '' || $pass2 === '') {
|
|
$err = "Semua field harus diisi.";
|
|
} elseif ($pass !== $pass2) {
|
|
$err = "Konfirmasi password tidak cocok.";
|
|
} elseif (!password_valid($pass)) {
|
|
$err = "Password minimal 6 karakter, harus mengandung huruf & angka.";
|
|
} else {
|
|
try {
|
|
// 3. Hash Password (Keamanan Kritis!)
|
|
$hash = password_hash($pass, PASSWORD_DEFAULT);
|
|
|
|
// 4. Prepared Statement (Mencegah SQL Injection)
|
|
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
|
$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 {
|
|
$err = "Error: Terjadi kesalahan saat registrasi database.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html lang="id">
|
|
<head>
|
|
<meta charset="UTF-8">
|
|
<title>Register</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:#28a745; color:white; border:none; border-radius:8px; cursor:pointer; }
|
|
.err { color:#d00000; margin-bottom:10px; text-align:center; }
|
|
.ok { color:green; margin-bottom:10px; text-align:center; }
|
|
.link { text-align:center; margin-top:10px; }
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<div class="card">
|
|
<h2>Register</h2>
|
|
|
|
|
|
<form method="POST" action="register.php">
|
|
<input class="input" type="text" name="username" placeholder="Masukkan Username">
|
|
<input class="input" type="password" name="password" placeholder="Masukkan Password">
|
|
<input class="input" type="password" name="password_confirm" placeholder="Konfirmasi Password">
|
|
<button class="btn" type="submit" name="register">Daftar</button>
|
|
</form>
|
|
|
|
<div class="link">
|
|
Sudah punya akun? <a href="login.php">Login</a>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|