dari HTML if (isset($_POST['btn-register'])) { $username = trim($_POST['username']); $email = trim($_POST['email']); $password = $_POST['password']; $confirm = $_POST['confirm_password']; // --- VALIDASI DASAR --- if (empty($username) || empty($email) || empty($password) || empty($confirm)) { header("Location: index.php?register_error=Data tidak boleh kosong"); exit; } if (!filter_var($email, FILTER_VALIDATE_EMAIL)) { header("Location: index.php?register_error=Format email tidak valid"); exit; } if (strlen($password) < 6) { header("Location: index.php?register_error=Password minimal 6 karakter"); exit; } if ($password !== $confirm) { header("Location: index.php?register_error=Konfirmasi password tidak cocok"); exit; } // --- CEK DATABASE (USER SUDAH ADA?) --- $cek = $conn->prepare("SELECT id FROM users WHERE username=? OR email=?"); $cek->bind_param("ss", $username, $email); $cek->execute(); $cek->store_result(); if ($cek->num_rows > 0) { header("Location: index.php?register_error=Username atau Email sudah terdaftar!"); exit; } $cek->close(); // --- INSERT DATA BARU --- $hash = password_hash($password, PASSWORD_DEFAULT); $insert = $conn->prepare("INSERT INTO users (username,email,password) VALUES (?,?,?)"); $insert->bind_param("sss", $username, $email, $hash); if ($insert->execute()) { // TUTUP DISINI (Sebelum pindah halaman) $insert->close(); header("Location: index.php?success=register"); exit; } else { // TUTUP DISINI JUGA (Sebelum pindah halaman) $insert->close(); header("Location: index.php?register_error=Gagal mendaftar, coba lagi nanti."); exit; } } /* ===================================================== LOGIN ==================================================== */ if (isset($_POST['btn-login'])) { $username = $_POST['username']; $password = $_POST['password']; $stmt = $conn->prepare("SELECT * FROM users WHERE username=?"); $stmt->bind_param("s", $username); $stmt->execute(); $result = $stmt->get_result(); $user = $result->fetch_assoc(); // Cek Password if (!$user || !password_verify($password, $user['password'])) { header("Location: index.php?error=gagal"); exit; } // Login Sukses $_SESSION['user'] = $user; header("Location: mainboard.php"); exit; } ?>