178 lines
5.4 KiB
PHP
178 lines
5.4 KiB
PHP
<?php
|
|
include "koneksi.php";
|
|
session_start();
|
|
|
|
/* ==========================================================
|
|
======================= LOGIN ============================
|
|
========================================================== */
|
|
|
|
$error = "";
|
|
if (isset($_POST['login'])) {
|
|
$username = mysqli_real_escape_string($conn, $_POST['username']);
|
|
$password = $_POST['password'];
|
|
|
|
// PENTING: ambil kolom bank, bukan balance
|
|
$sql = "SELECT id, username, password, bank FROM users WHERE username = ?";
|
|
$stmt = mysqli_prepare($conn, $sql);
|
|
mysqli_stmt_bind_param($stmt, "s", $username);
|
|
mysqli_stmt_execute($stmt);
|
|
$result = mysqli_stmt_get_result($stmt);
|
|
|
|
if (mysqli_num_rows($result) > 0) {
|
|
$user = mysqli_fetch_assoc($result);
|
|
|
|
if ($password === $user['password']) {
|
|
|
|
// Set SESSION
|
|
$_SESSION['user_id'] = $user['id'];
|
|
$_SESSION['username'] = $user['username'];
|
|
$_SESSION['bank'] = intval($user['bank']); // PASTIKAN integer
|
|
|
|
// Update last login
|
|
$update_sql = "UPDATE users SET last_login = NOW() WHERE id = ?";
|
|
$update_stmt = mysqli_prepare($conn, $update_sql);
|
|
mysqli_stmt_bind_param($update_stmt, "i", $user['id']);
|
|
mysqli_stmt_execute($update_stmt);
|
|
|
|
// Masuk ke game page
|
|
header("Location: html.php");
|
|
exit;
|
|
} else {
|
|
$error = "Invalid username or password";
|
|
}
|
|
} else {
|
|
$error = "Invalid username or password";
|
|
}
|
|
mysqli_stmt_close($stmt);
|
|
}
|
|
|
|
/* ==========================================================
|
|
======================= REGISTER ==========================
|
|
========================================================== */
|
|
|
|
$success = "";
|
|
if (isset($_POST['register'])) {
|
|
|
|
$username = mysqli_real_escape_string($conn, $_POST['username']);
|
|
$password = $_POST['password'];
|
|
$confirm = $_POST['confirm_password'];
|
|
|
|
if (empty($username) || empty($password)) {
|
|
$error = "All fields are required.";
|
|
} elseif ($password !== $confirm) {
|
|
$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 {
|
|
|
|
// simpan password plain text (testing)
|
|
$hashed = $password;
|
|
|
|
// Insert user baru — gunakan kolom bank!
|
|
$insert_sql = "INSERT INTO users (username, password, bank, created_at)
|
|
VALUES (?, ?, 1000, NOW())";
|
|
$insert_stmt = mysqli_prepare($conn, $insert_sql);
|
|
mysqli_stmt_bind_param($insert_stmt, "ss", $username, $hashed);
|
|
|
|
if (mysqli_stmt_execute($insert_stmt)) {
|
|
|
|
$success = "Registration successful. You may login.";
|
|
|
|
// Auto-login
|
|
$new_id = mysqli_insert_id($conn);
|
|
$_SESSION['user_id'] = $new_id;
|
|
$_SESSION['username'] = $username;
|
|
$_SESSION['bank'] = 1000;
|
|
|
|
header("Location: html.php");
|
|
exit;
|
|
} else {
|
|
$error = "Registration failed. Try again.";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
?>
|
|
|
|
<!DOCTYPE html>
|
|
<html>
|
|
<head>
|
|
<title>Login / Register</title>
|
|
<link rel="stylesheet" href="login.css">
|
|
</head>
|
|
|
|
<body>
|
|
<div class="container">
|
|
|
|
<div class="logo">
|
|
<h1>OCAGamingHub</h1>
|
|
<p>Sign in or create 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; ?>
|
|
|
|
<!-- ================== LOGIN FORM ================== -->
|
|
<form method="POST">
|
|
<h2>Login</h2>
|
|
<div class="form-group">
|
|
<label>Username</label>
|
|
<input type="text" name="username">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Password</label>
|
|
<input type="password" name="password">
|
|
</div>
|
|
|
|
<button type="submit" name="login" class="btn btn-signin">Login</button>
|
|
</form>
|
|
|
|
<hr>
|
|
|
|
<!-- ================== REGISTER FORM ================== -->
|
|
<form method="POST">
|
|
<h2>Register</h2>
|
|
|
|
<div class="form-group">
|
|
<label>Username</label>
|
|
<input type="text" name="username">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Password</label>
|
|
<input type="password" name="password">
|
|
</div>
|
|
|
|
<div class="form-group">
|
|
<label>Confirm Password</label>
|
|
<input type="password" name="confirm_password">
|
|
</div>
|
|
|
|
<button type="submit" name="register" class="btn btn-signup">Create Account</button>
|
|
</form>
|
|
|
|
</div>
|
|
</div>
|
|
</body>
|
|
</html>
|