Kelompok02-Memory-Card/register.php
2025-12-08 08:49:59 +07:00

56 lines
1.4 KiB
PHP

<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
$username = trim($_POST["username"]);
$email = trim($_POST["email"]);
$password = $_POST["password"];
$confirm = $_POST["confirm"];
if (!$username || !$email || !$password || !$confirm) {
echo "Semua field wajib diisi";
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Email tidak valid";
exit;
}
if ($password !== $confirm) {
echo "Password tidak cocok";
exit;
}
$file = "users.json";
$users = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
// 🔍 Cek username & email duplikat
foreach ($users as $u) {
if (strtolower($u["username"]) === strtolower($username)) {
echo "Username sudah digunakan";
exit;
}
if (strtolower($u["email"]) === strtolower($email)) {
echo "Email sudah digunakan";
exit;
}
}
// 🔐 Enkripsi password
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
// 📌 tambah user baru
$users[] = [
"id" => uniqid(),
"username" => $username,
"email" => $email,
"password" => $hashedPassword,
"role" => "player",
"created_at" => date("Y-m-d H:i:s")
];
file_put_contents($file, json_encode($users, JSON_PRETTY_PRINT));
echo "success";
}
?>