Merge branch 'main' of https://git-eng.ukwms.ac.id/2526-web/Kelompok02-Memory-Card
This commit is contained in:
commit
a3b084dec7
90
auth.php
90
auth.php
@ -1,74 +1,63 @@
|
|||||||
<?php
|
<?php
|
||||||
session_start();
|
session_start();
|
||||||
require_once "Config.php";
|
require_once "Config.php";
|
||||||
|
|
||||||
// ==========================================
|
// ===================== REGISTER =====================
|
||||||
// BAGIAN 1: LOGIKA REGISTER
|
|
||||||
// ==========================================
|
|
||||||
if (isset($_POST['btn-register'])) {
|
if (isset($_POST['btn-register'])) {
|
||||||
|
|
||||||
$username = $_POST['username'];
|
$username = trim($_POST['username']);
|
||||||
$email = $_POST['email'];
|
$email = trim($_POST['email']);
|
||||||
$password = $_POST['password'];
|
$password = $_POST['password'];
|
||||||
$confirm = $_POST['confirm_password'];
|
$confirm = $_POST['confirm_password'];
|
||||||
|
|
||||||
// --- 1. VALIDASI DATA ---
|
// validasi
|
||||||
|
if (!$username || !$email || !$password || !$confirm) {
|
||||||
// Cek Kosong
|
header("Location: index.php?error=kosong");
|
||||||
if (empty($username) || empty($email) || empty($password) || empty($confirm)) {
|
|
||||||
echo "<script>alert('Semua data wajib diisi!'); window.location='index.php';</script>";
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cek Format Email (Biar gak ngawur)
|
|
||||||
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
||||||
echo "<script>alert('Format email tidak valid! (contoh: nama@email.com)'); window.location='index.php';</script>";
|
header("Location: index.php?error=email");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cek Panjang Password (Minimal 6)
|
|
||||||
if (strlen($password) < 6) {
|
if (strlen($password) < 6) {
|
||||||
echo "<script>alert('Password terlalu pendek! Minimal 6 karakter.'); window.location='index.php';</script>";
|
header("Location: index.php?error=pass");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cek Kesamaan Password
|
|
||||||
if ($password !== $confirm) {
|
if ($password !== $confirm) {
|
||||||
echo "<script>alert('Password dan Konfirmasi tidak cocok!'); window.location='index.php';</script>";
|
header("Location: index.php?error=confirm");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- 2. CEK DUPLIKAT DI DATABASE ---
|
// cek user
|
||||||
$stmt = mysqli_prepare($conn, "SELECT id FROM users WHERE username = ? OR email = ?");
|
$cek = mysqli_prepare($conn, "SELECT id FROM users WHERE username=? OR email=?");
|
||||||
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
|
mysqli_stmt_bind_param($cek, "ss", $username, $email);
|
||||||
mysqli_stmt_execute($stmt);
|
mysqli_stmt_execute($cek);
|
||||||
mysqli_stmt_store_result($stmt);
|
mysqli_stmt_store_result($cek);
|
||||||
|
|
||||||
if (mysqli_stmt_num_rows($stmt) > 0) {
|
if (mysqli_stmt_num_rows($cek) > 0) {
|
||||||
echo "<script>alert('Username atau Email sudah terpakai! Ganti yang lain.'); window.location='index.php';</script>";
|
header("Location: index.php?error=exist");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
mysqli_stmt_close($stmt);
|
|
||||||
|
|
||||||
// --- 3. SIMPAN DATA ---
|
mysqli_stmt_close($cek);
|
||||||
$hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
||||||
$stmtInsert = mysqli_prepare($conn, "INSERT INTO users (username, email, password) VALUES (?, ?, ?)");
|
|
||||||
mysqli_stmt_bind_param($stmtInsert, "sss", $username, $email, $hashed_password);
|
|
||||||
|
|
||||||
if (mysqli_stmt_execute($stmtInsert)) {
|
// simpan
|
||||||
echo "<script>alert('Registrasi Berhasil! Silakan Login.'); window.location='index.php';</script>";
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
||||||
} else {
|
$insert = mysqli_prepare($conn, "INSERT INTO users (username,email,password) VALUES (?,?,?)");
|
||||||
echo "Error: " . mysqli_error($conn);
|
mysqli_stmt_bind_param($insert, "sss", $username, $email, $hash);
|
||||||
}
|
mysqli_stmt_execute($insert);
|
||||||
mysqli_stmt_close($stmtInsert);
|
mysqli_stmt_close($insert);
|
||||||
|
|
||||||
}
|
header("Location: index.php?success=register");
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ===================== LOGIN =====================
|
||||||
|
if (isset($_POST['btn-login'])) {
|
||||||
|
|
||||||
// ==========================================
|
|
||||||
// BAGIAN 2: LOGIKA LOGIN
|
|
||||||
// ==========================================
|
|
||||||
else if (isset($_POST['btn-login'])) {
|
|
||||||
|
|
||||||
$username = $_POST['username'];
|
$username = $_POST['username'];
|
||||||
$password = $_POST['password'];
|
$password = $_POST['password'];
|
||||||
|
|
||||||
@ -77,20 +66,15 @@ else if (isset($_POST['btn-login'])) {
|
|||||||
mysqli_stmt_execute($stmt);
|
mysqli_stmt_execute($stmt);
|
||||||
|
|
||||||
$result = mysqli_stmt_get_result($stmt);
|
$result = mysqli_stmt_get_result($stmt);
|
||||||
$row = mysqli_fetch_assoc($result);
|
$user = mysqli_fetch_assoc($result);
|
||||||
|
|
||||||
// JIKA GAGAL
|
if (!$user || !password_verify($password, $user['password'])) {
|
||||||
if (!$row || !password_verify($password, $row['password'])) {
|
header("Location: index.php?error=login");
|
||||||
// Kirim sinyal error ke HTML (Kotak Merah)
|
|
||||||
header("Location: index.php?error=gagal");
|
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
// JIKA SUKSES
|
$_SESSION['user'] = $user;
|
||||||
$_SESSION['username'] = $row['username'];
|
|
||||||
$_SESSION['login'] = true;
|
|
||||||
|
|
||||||
header("Location: mainboard.html");
|
header("Location: mainboard.php");
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
?>
|
|
||||||
Loading…
x
Reference in New Issue
Block a user