90 lines
2.9 KiB
PHP
90 lines
2.9 KiB
PHP
<?php
|
|
// Mulai session paling atas (wajib untuk login)
|
|
session_start();
|
|
|
|
// Panggil koneksi database sekali saja
|
|
require_once "Config.php";
|
|
|
|
// ==========================================
|
|
// BAGIAN 1: LOGIKA REGISTER
|
|
// ==========================================
|
|
if (isset($_POST['btn-register'])) {
|
|
$username = $_POST['username'];
|
|
$email = $_POST['email'];
|
|
$password = $_POST['password'];
|
|
$confirm = $_POST['confirm_password'];
|
|
|
|
// Validasi input kosong
|
|
if (empty($username) || empty($email) || empty($password) || empty($confirm)) {
|
|
echo "<script>alert('Semua data harus diisi!'); window.location='index.html';</script>";
|
|
exit;
|
|
}
|
|
|
|
// Validasi password match
|
|
if ($password !== $confirm) {
|
|
echo "<script>alert('Password dan Konfirmasi tidak cocok!'); window.location='index.html';</script>";
|
|
exit;
|
|
}
|
|
|
|
// Cek Username/Email sudah ada atau belum
|
|
$stmt = mysqli_prepare($conn, "SELECT id FROM users WHERE username = ? OR email = ?");
|
|
mysqli_stmt_bind_param($stmt, "ss", $username, $email);
|
|
mysqli_stmt_execute($stmt);
|
|
mysqli_stmt_store_result($stmt);
|
|
|
|
if (mysqli_stmt_num_rows($stmt) > 0) {
|
|
echo "<script>alert('Username atau Email sudah terpakai!'); window.location='index.html';</script>";
|
|
exit; // Stop di sini
|
|
}
|
|
mysqli_stmt_close($stmt);
|
|
|
|
// Hash password & Insert
|
|
$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)) {
|
|
// Balik ke index.html tapi kasih pesan sukses
|
|
echo "<script>alert('Registrasi Berhasil! Silakan Login.'); window.location='index.html';</script>";
|
|
} else {
|
|
echo "Error: " . mysqli_error($conn);
|
|
}
|
|
mysqli_stmt_close($stmtInsert);
|
|
|
|
}
|
|
|
|
// ==========================================
|
|
// BAGIAN 2: LOGIKA LOGIN
|
|
// ==========================================
|
|
else if (isset($_POST['btn-login'])) {
|
|
|
|
$username = $_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);
|
|
|
|
$result = mysqli_stmt_get_result($stmt);
|
|
$row = mysqli_fetch_assoc($result);
|
|
|
|
// Cek user ada ATAU password salah
|
|
if (!$row || !password_verify($password, $row['password'])) {
|
|
echo "<script>
|
|
alert('Username atau Password salah!');
|
|
window.location.href='index.html';
|
|
</script>";
|
|
exit;
|
|
}
|
|
|
|
// Login Sukses
|
|
$_SESSION['username'] = $row['username'];
|
|
$_SESSION['login'] = true;
|
|
|
|
echo "<script>
|
|
alert('Login Berhasil! Selamat Datang, " . $username . "');
|
|
window.location.href='mainboard.html';
|
|
</script>";
|
|
exit;
|
|
}
|
|
?>
|