Compare commits

..

No commits in common. "77a89683d44aa6ae0a9242ff426e562a67ce8734" and "d753faf96d668c136d3f27e5ce4cb9e4deea06cb" have entirely different histories.

2 changed files with 30 additions and 42 deletions

View File

@ -20,69 +20,54 @@ if (isset($_POST['btn-register'])) {
$password = $_POST['password']; $password = $_POST['password'];
$confirm = $_POST['confirm_password']; $confirm = $_POST['confirm_password'];
// --- VALIDASI DASAR --- // Validasi sederhana
if (empty($username) || empty($email) || empty($password) || empty($confirm)) {
if (!$username || !$email || !$password || !$confirm) { $_SESSION['error'] = "Semua kolom wajib diisi!";
// Balik ke index dengan pesan error header("Location: index.php");
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; exit;
} }
if ($password !== $confirm) { if ($password !== $confirm) {
header("Location: index.php?register_error=Konfirmasi password tidak cocok"); $_SESSION['error'] = "Konfirmasi password tidak cocok!";
header("Location: index.php");
exit; exit;
} }
// --- CEK DATABASE (USER SUDAH ADA?) --- // Cek user sudah ada atau belum
$cek = $conn->prepare("SELECT id FROM users WHERE username=? OR email=?"); $cek = $conn->prepare("SELECT id FROM users WHERE username=? OR email=?");
$cek->bind_param("ss", $username, $email); $cek->bind_param("ss", $username, $email);
$cek->execute(); $cek->execute();
$cek->store_result(); $cek->store_result();
if ($cek->num_rows > 0) { if ($cek->num_rows > 0) {
// INI YANG SEBELUMNYA MATI, SEKARANG REDIRECT: $_SESSION['error'] = "Username atau Email sudah terdaftar!";
header("Location: index.php?register_error=Username atau Email sudah terdaftar!"); header("Location: index.php");
exit; exit;
} }
$cek->close(); $cek->close();
// --- INSERT DATA BARU --- // Insert ke database
$hash = password_hash($password, PASSWORD_DEFAULT); $hash = password_hash($password, PASSWORD_DEFAULT);
$insert = $conn->prepare("INSERT INTO users (username,email,password) VALUES (?,?,?)"); $insert = $conn->prepare("INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
$insert->bind_param("sss", $username, $email, $hash); $insert->bind_param("sss", $username, $email, $hash);
if ($insert->execute()) { if ($insert->execute()) {
// Register Berhasil -> Arahkan ke Login (atau mainboard) $_SESSION['success'] = "Registrasi berhasil! Silakan login.";
// Kita kosongkan error agar masuk ke state normal
header("Location: index.php");
exit;
} else { } else {
header("Location: index.php?register_error=Gagal mendaftar, coba lagi nanti."); $_SESSION['error'] = "Terjadi kesalahan sistem: " . $conn->error;
exit;
} }
$insert->close(); $insert->close();
header("Location: index.php"); // Kembali ke index
exit;
} }
/* ===================================================== /* =====================================================
LOGIN LOGIN
==================================================== */ ===================================================== */
if (isset($_POST['btn-login'])) { if (isset($_POST['btn-login'])) {
$username = $_POST['username']; $username = trim($_POST['username']);
$password = $_POST['password']; $password = $_POST['password'];
$stmt = $conn->prepare("SELECT * FROM users WHERE username=?"); $stmt = $conn->prepare("SELECT * FROM users WHERE username=?");
@ -92,16 +77,16 @@ if (isset($_POST['btn-login'])) {
$result = $stmt->get_result(); $result = $stmt->get_result();
$user = $result->fetch_assoc(); $user = $result->fetch_assoc();
// Cek Password if ($user && password_verify($password, $user['password'])) {
if (!$user || !password_verify($password, $user['password'])) { // Login Sukses
// Redirect dengan parameter 'error=gagal' agar ditangkap JS Login $_SESSION['user'] = $user;
header("Location: index.php?error=gagal"); header("Location: mainboard.php"); // Pastikan file ini ada!
exit;
} else {
// Login Gagal
$_SESSION['error'] = "Username atau Password salah!";
header("Location: index.php");
exit; exit;
} }
// Login Sukses
$_SESSION['user'] = $user;
header("Location: mainboard.php");
exit;
} }
?> ?>

View File

@ -4,9 +4,11 @@
<meta charset="UTF-8"> <meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Login & Register - Memory Game</title> <title>Login & Register - Memory Game</title>
<link rel="stylesheet" href="/Kelompok02-Memory-Card/assets/style.css"> <link rel="stylesheet" href="/Kelompok02-Memory-Card/assets/style.css">
</head> </head>
<body> <body>
<img src="images/fruit1.png" class="fruit f1"><img src="images/fruit2.png" class="fruit f2"> <img src="images/fruit1.png" class="fruit f1"><img src="images/fruit2.png" class="fruit f2">
<img src="images/fruit3.png" class="fruit f3"><img src="images/fruit4.png" class="fruit f4"> <img src="images/fruit3.png" class="fruit f3"><img src="images/fruit4.png" class="fruit f4">
<img src="images/fruit5.png" class="fruit f5"><img src="images/fruit6.png" class="fruit f6"> <img src="images/fruit5.png" class="fruit f5"><img src="images/fruit6.png" class="fruit f6">
@ -16,6 +18,7 @@
<div class="auth-card" id="authCard"> <div class="auth-card" id="authCard">
<div class="form-wrapper"> <div class="form-wrapper">
<div class="forms-container"> <div class="forms-container">
<form id="loginForm" action="auth.php" method="POST"> <form id="loginForm" action="auth.php" method="POST">
<h2>Selamat Datang! </h2> <h2>Selamat Datang! </h2>
<p class="subtitle">Login untuk bermain</p> <p class="subtitle">Login untuk bermain</p>