This commit is contained in:
ody 2025-12-03 18:28:08 +07:00
parent 5ec1bd97de
commit 13c0a8a7dd

View File

@ -1,141 +1,178 @@
<?php <?php
include 'koneksi.php'; include "koneksi.php";
session_start();
$success = false; /* ==========================================================
======================= LOGIN ============================
========================================================== */
if (isset($_POST['register'])) { $error = "";
$username = $_POST['username']; if (isset($_POST['login'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = $_POST['password']; $password = $_POST['password'];
// basic escaping to avoid simple injection (keep consistent with existing style) // PENTING: ambil kolom bank, bukan balance
$username = mysqli_real_escape_string($conn, $username); $sql = "SELECT id, username, password, bank FROM users WHERE username = ?";
$password = mysqli_real_escape_string($conn, $password); $stmt = mysqli_prepare($conn, $sql);
mysqli_stmt_bind_param($stmt, "s", $username);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
// insert with initial balance = 0 if (mysqli_num_rows($result) > 0) {
$SQL = "INSERT INTO users (username, password, balance) VALUES ('$username', '$password', 0)"; $user = mysqli_fetch_assoc($result);
$result = mysqli_query($conn, $SQL);
if ($result) { if ($password === $user['password']) {
$success = true;
// 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> <!DOCTYPE html>
<html lang="en"> <html>
<head> <head>
<meta charset="UTF-8"> <title>Login / Register</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OCA Gaming Hub - Login</title>
<link rel="stylesheet" href="login.css"> <link rel="stylesheet" href="login.css">
</head> </head>
<body> <body>
<div class="container"> <div class="container">
<div class="logo"> <div class="logo">
<h1> OCA GAMING HUB </h1> <h1>OCAGamingHub</h1>
<p>BLACKJACK 21 CARD GAME</p> <p>Sign in or create account</p>
</div> </div>
<div class="form-container"> <div class="form-container">
<div class="card-icon">🂡</div>
<?php if ($success): ?> <?php if (!empty($error)): ?>
<div class="success-message show">Register Success!</div> <div class="error-message show"><?= htmlspecialchars($error) ?></div>
<script>
setTimeout(function() {
window.location.href = 'loginn.php';
}, 2000);
</script>
<?php else: ?>
<!-- Sign Up Page -->
<form action ="register.php" method="POST">
<div class="form-group">
<label for="username">Username</label>
<input id="username" type="text" name="username" required>
</div>
<div class="form-group">
<label for="password">Password</label>
<input id="password" type="password" name="password" required>
</div>
<div class="button-group">
<button type="submit" name="register" class="btn btn-register">Register</button>
</div>
</form>
<?php endif; ?> <?php endif; ?>
</div>
<?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>
<script> <div class="form-group">
function goToMain() { <label>Password</label>
document.getElementById('mainPage').style.display = 'block'; <input type="password" name="password">
document.getElementById('signupForm').style.display = 'none'; </div>
// Clear messages
document.getElementById('mainMessage').classList.remove('show');
document.getElementById('mainError').classList.remove('show');
}
function goToSignUp() { <button type="submit" name="login" class="btn btn-signin">Login</button>
document.getElementById('mainPage').style.display = 'none'; </form>
document.getElementById('signupForm').style.display = 'block';
}
// Login handler: validate input and show messages <hr>
function handleLogin() {
const username = document.getElementById('mainUsername').value.trim();
const password = document.getElementById('mainPassword').value.trim();
const successEl = document.getElementById('mainMessage');
const errorEl = document.getElementById('mainError');
// Reset messages <!-- ================== REGISTER FORM ================== -->
successEl.classList.remove('show'); <form method="POST">
errorEl.classList.remove('show'); <h2>Register</h2>
if (!username || !password) { <div class="form-group">
errorEl.textContent = 'Please enter both username and password.'; <label>Username</label>
errorEl.classList.add('show'); <input type="text" name="username">
return; </div>
}
// Simulate login (replace with real auth as needed) <div class="form-group">
if (username.toLowerCase() === 'admin' && password === 'admin') { <label>Password</label>
successEl.textContent = `Welcome back, ${username}! Redirecting...`; <input type="password" name="password">
successEl.classList.add('show'); </div>
setTimeout(() => {
alert('Logged in as ' + username + '. (Simulated)');
// Example: redirect to game/dashboard page
// window.location.href = 'dashboard.html';
}, 800);
} else {
errorEl.textContent = 'Invalid username or password.';
errorEl.classList.add('show');
}
}
// Signup Form Handler <div class="form-group">
document.getElementById('signupForm').addEventListener('submit', function (e) { <label>Confirm Password</label>
e.preventDefault(); <input type="password" name="confirm_password">
const username = document.getElementById('signupUsername').value; </div>
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
if (username && email && password) { <button type="submit" name="register" class="btn btn-signup">Create Account</button>
const message = document.getElementById('signupMessage'); </form>
message.textContent = `✓ Account created successfully for ${username}!`;
message.classList.add('show');
setTimeout(() => { </div>
alert(`Account created!\nUsername: ${username}\nEmail: ${email}`); </div>
// Add your redirect here
goToMain();
}, 1500);
}
});
</script>
</body> </body>
</html> </html>