54 lines
1.8 KiB
PHP
54 lines
1.8 KiB
PHP
<?php
|
|
session_start();
|
|
include "koneksi.php";
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$username = trim($_POST['username']);
|
|
$email = trim($_POST['email']);
|
|
$password = trim($_POST['password']);
|
|
$confirmPassword = trim($_POST['confirm_password']);
|
|
|
|
if (!$username || !$email || !$password || !$confirmPassword) {
|
|
echo "<script>alert('Semua field harus diisi'); window.history.back();</script>";
|
|
exit;
|
|
}
|
|
|
|
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
|
|
echo "<script>alert('Format email tidak valid'); window.history.back();</script>";
|
|
exit;
|
|
}
|
|
|
|
if ($password !== $confirmPassword) {
|
|
echo "<script>alert('Password dan konfirmasi password tidak cocok'); window.history.back();</script>";
|
|
exit;
|
|
}
|
|
|
|
if (strlen($password) < 6) {
|
|
echo "<script>alert('Password minimal 6 karakter'); window.history.back();</script>";
|
|
exit;
|
|
}
|
|
|
|
// cek username & email di database
|
|
$stmt = mysqli_prepare($conn, "SELECT id FROM user 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 digunakan'); window.history.back();</script>";
|
|
exit;
|
|
}
|
|
|
|
// hash password
|
|
$hash = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
// insert user
|
|
$stmt = mysqli_prepare($conn, "INSERT INTO user (username, email, password, role) VALUES (?, ?, ?, 'player')");
|
|
mysqli_stmt_bind_param($stmt, "sss", $username, $email, $hash);
|
|
mysqli_stmt_execute($stmt);
|
|
|
|
echo "<script>alert('Registrasi berhasil! Silakan login.'); window.location.href='login.html';</script>";
|
|
exit;
|
|
}
|
|
?>
|