diff --git a/Config.php b/Config.php index 87aba80..c6c537f 100644 --- a/Config.php +++ b/Config.php @@ -1,12 +1,13 @@ connect_error) { + die("Koneksi gagal: " . $conn->connect_error); } ?> diff --git a/auth.php b/auth.php index 1b8d5d1..78e2417 100644 --- a/auth.php +++ b/auth.php @@ -1,8 +1,18 @@ prepare("SELECT id FROM users WHERE username=? OR email=?"); + $cek->bind_param("ss", $username, $email); + $cek->execute(); + $cek->store_result(); - if (mysqli_stmt_num_rows($cek) > 0) { - header("Location: index.php?error=exist"); + if ($cek->num_rows > 0) { + $_SESSION['error'] = "Username atau Email sudah terdaftar!"; + header("Location: index.php"); exit; } + $cek->close(); - mysqli_stmt_close($cek); - - // simpan + // Insert ke database $hash = password_hash($password, PASSWORD_DEFAULT); - $insert = mysqli_prepare($conn, "INSERT INTO users (username,email,password) VALUES (?,?,?)"); - mysqli_stmt_bind_param($insert, "sss", $username, $email, $hash); - mysqli_stmt_execute($insert); - mysqli_stmt_close($insert); + $insert = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)"); + $insert->bind_param("sss", $username, $email, $hash); - header("Location: index.php?success=register"); + if ($insert->execute()) { + $_SESSION['success'] = "Registrasi berhasil! Silakan login."; + } else { + $_SESSION['error'] = "Terjadi kesalahan sistem: " . $conn->error; + } + + $insert->close(); + header("Location: index.php"); // Kembali ke index exit; } -// ===================== LOGIN ===================== +/* ===================================================== + LOGIN +===================================================== */ if (isset($_POST['btn-login'])) { - $username = $_POST['username']; + $username = trim($_POST['username']); $password = $_POST['password']; - $stmt = mysqli_prepare($conn, "SELECT * FROM users WHERE username=?"); - mysqli_stmt_bind_param($stmt, "s", $username); - mysqli_stmt_execute($stmt); + $stmt = $conn->prepare("SELECT * FROM users WHERE username=?"); + $stmt->bind_param("s", $username); + $stmt->execute(); - $result = mysqli_stmt_get_result($stmt); - $user = mysqli_fetch_assoc($result); + $result = $stmt->get_result(); + $user = $result->fetch_assoc(); - if (!$user || !password_verify($password, $user['password'])) { - header("Location: index.php?error=login"); + if ($user && password_verify($password, $user['password'])) { + // Login Sukses + $_SESSION['user'] = $user; + header("Location: mainboard.php"); // Pastikan file ini ada! + exit; + } else { + // Login Gagal + $_SESSION['error'] = "Username atau Password salah!"; + header("Location: index.php"); exit; } - - $_SESSION['user'] = $user; - - header("Location: mainboard.php"); - exit; } +?> \ No newline at end of file