105 lines
3.7 KiB
PHP
105 lines
3.7 KiB
PHP
<?php
|
|
include "koneksi.php";
|
|
session_start();
|
|
|
|
$error = '';
|
|
$success = '';
|
|
|
|
if(isset($_POST['register'])){
|
|
$username = mysqli_real_escape_string($conn, $_POST['username']);
|
|
$password = $_POST['password'];
|
|
$confirm_password = $_POST['confirm_password'];
|
|
|
|
// Validasi
|
|
if(empty($username) || empty($password)) {
|
|
$error = 'All fields are required.';
|
|
} elseif($password !== $confirm_password) {
|
|
$error = 'Passwords do not match.';
|
|
} elseif(strlen($password) < 6) {
|
|
$error = 'Password must be at least 6 characters.';
|
|
} else {
|
|
// Cek username sudah ada
|
|
$check_sql = "SELECT id FROM users WHERE username = ?";
|
|
$check_stmt = mysqli_prepare($conn, $check_sql);
|
|
mysqli_stmt_bind_param($check_stmt, "s", $username);
|
|
mysqli_stmt_execute($check_stmt);
|
|
mysqli_stmt_store_result($check_stmt);
|
|
|
|
if(mysqli_stmt_num_rows($check_stmt) > 0) {
|
|
$error = 'Username already exists.';
|
|
} else {
|
|
// Password hashing untuk keamanan
|
|
// UNTUK TESTING: simpan plain text (tidak direkomendasikan)
|
|
$hashed_password = $password; // HAPUS INI DI PRODUKSI
|
|
|
|
// UNTUK PRODUKSI: gunakan password_hash()
|
|
// $hashed_password = password_hash($password, PASSWORD_DEFAULT);
|
|
|
|
// Insert user baru dengan saldo awal
|
|
$insert_sql = "INSERT INTO users (username, password, balance, created_at)
|
|
VALUES (?, ?, 1000, NOW())";
|
|
$insert_stmt = mysqli_prepare($conn, $insert_sql);
|
|
mysqli_stmt_bind_param($insert_stmt, "ss", $username, $hashed_password);
|
|
|
|
if(mysqli_stmt_execute($insert_stmt)) {
|
|
$success = 'Registration successful! You can now login.';
|
|
// Auto login setelah register (opsional)
|
|
$user_id = mysqli_insert_id($conn);
|
|
$_SESSION['user_id'] = $user_id;
|
|
$_SESSION['username'] = $username;
|
|
$_SESSION['balance'] = 1000;
|
|
|
|
header("Location: html.php");
|
|
exit;
|
|
} else {
|
|
$error = 'Registration failed. Please try again.';
|
|
}
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Register</title>
|
|
<link rel="stylesheet" href="login.css">
|
|
</head>
|
|
<body>
|
|
<div class="container">
|
|
<div class="logo">
|
|
<h1>OCAGamingHub</h1>
|
|
<p>Create your account</p>
|
|
</div>
|
|
|
|
<div class="form-container">
|
|
<?php if(!empty($error)): ?>
|
|
<div class="error-message show"><?=htmlspecialchars($error)?></div>
|
|
<?php endif; ?>
|
|
|
|
<?php if(!empty($success)): ?>
|
|
<div class="success-message show"><?=htmlspecialchars($success)?></div>
|
|
<?php endif; ?>
|
|
|
|
<form method="POST">
|
|
<div class="form-group">
|
|
<label>Username</label>
|
|
<input type="text" name="username" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Password</label>
|
|
<input type="password" name="password" required>
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Confirm Password</label>
|
|
<input type="password" name="confirm_password" required>
|
|
</div>
|
|
|
|
<button type="submit" name="register" class="btn btn-signin">Register</button>
|
|
<a href="loginn.php" class="btn btn-signup">Back to Login</a>
|
|
</form>
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|