Compare commits

..

No commits in common. "a8901fb771d5573728067a3edf5f038806e7a4a6" and "5ec1bd97de6d449ecb6cbc03251d8712a84b78f9" have entirely different histories.

2 changed files with 109 additions and 163 deletions

View File

@ -1,178 +1,141 @@
<?php <?php
include "koneksi.php"; include 'koneksi.php';
session_start();
/* ========================================================== $success = false;
======================= 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'])) { if (isset($_POST['register'])) {
$username = $_POST['username'];
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = $_POST['password']; $password = $_POST['password'];
$confirm = $_POST['confirm_password'];
if (empty($username) || empty($password)) { // basic escaping to avoid simple injection (keep consistent with existing style)
$error = "All fields are required."; $username = mysqli_real_escape_string($conn, $username);
} elseif ($password !== $confirm) { $password = mysqli_real_escape_string($conn, $password);
$error = "Passwords do not match.";
} elseif (strlen($password) < 6) {
$error = "Password must be at least 6 characters.";
} else {
// cek username sudah ada // insert with initial balance = 0
$check_sql = "SELECT id FROM users WHERE username = ?"; $SQL = "INSERT INTO users (username, password, balance) VALUES ('$username', '$password', 0)";
$check_stmt = mysqli_prepare($conn, $check_sql); $result = mysqli_query($conn, $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) { if ($result) {
$error = "Username already exists."; $success = true;
} 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> <html lang="en">
<head> <head>
<title>Login / Register</title> <meta charset="UTF-8">
<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>OCAGamingHub</h1> <h1> OCA GAMING HUB </h1>
<p>Sign in or create account</p> <p>BLACKJACK 21 CARD GAME</p>
</div> </div>
<div class="form-container"> <div class="form-container">
<div class="card-icon">🂡</div>
<?php if (!empty($error)): ?> <?php if ($success): ?>
<div class="error-message show"><?= htmlspecialchars($error) ?></div> <div class="success-message show">Register Success!</div>
<?php endif; ?> <script>
setTimeout(function() {
<?php if (!empty($success)): ?> window.location.href = 'loginn.php';
<div class="success-message show"><?= htmlspecialchars($success) ?></div> }, 2000);
<?php endif; ?> </script>
<?php else: ?>
<!-- ================== LOGIN FORM ================== --> <!-- Sign Up Page -->
<form method="POST"> <form action ="register.php" method="POST">
<h2>Login</h2>
<div class="form-group"> <div class="form-group">
<label>Username</label> <label for="username">Username</label>
<input type="text" name="username"> <input id="username" type="text" name="username" required>
</div> </div>
<div class="form-group"> <div class="form-group">
<label>Password</label> <label for="password">Password</label>
<input type="password" name="password"> <input id="password" type="password" name="password" required>
</div> </div>
<button type="submit" name="login" class="btn btn-signin">Login</button> <div class="button-group">
<button type="submit" name="register" class="btn btn-register">Register</button>
</div>
</form> </form>
<?php endif; ?>
<hr> </div>
<!-- ================== REGISTER FORM ================== -->
<form method="POST">
<h2>Register</h2>
<div class="form-group">
<label>Username</label>
<input type="text" name="username">
</div> </div>
<div class="form-group"> <script>
<label>Password</label> function goToMain() {
<input type="password" name="password"> document.getElementById('mainPage').style.display = 'block';
</div> document.getElementById('signupForm').style.display = 'none';
// Clear messages
document.getElementById('mainMessage').classList.remove('show');
document.getElementById('mainError').classList.remove('show');
}
<div class="form-group"> function goToSignUp() {
<label>Confirm Password</label> document.getElementById('mainPage').style.display = 'none';
<input type="password" name="confirm_password"> document.getElementById('signupForm').style.display = 'block';
</div> }
<button type="submit" name="register" class="btn btn-signup">Create Account</button> // Login handler: validate input and show messages
</form> 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');
</div> // Reset messages
</div> successEl.classList.remove('show');
errorEl.classList.remove('show');
if (!username || !password) {
errorEl.textContent = 'Please enter both username and password.';
errorEl.classList.add('show');
return;
}
// Simulate login (replace with real auth as needed)
if (username.toLowerCase() === 'admin' && password === 'admin') {
successEl.textContent = `Welcome back, ${username}! Redirecting...`;
successEl.classList.add('show');
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
document.getElementById('signupForm').addEventListener('submit', function (e) {
e.preventDefault();
const username = document.getElementById('signupUsername').value;
const email = document.getElementById('signupEmail').value;
const password = document.getElementById('signupPassword').value;
if (username && email && password) {
const message = document.getElementById('signupMessage');
message.textContent = `✓ Account created successfully for ${username}!`;
message.classList.add('show');
setTimeout(() => {
alert(`Account created!\nUsername: ${username}\nEmail: ${email}`);
// Add your redirect here
goToMain();
}, 1500);
}
});
</script>
</body> </body>
</html> </html>

View File

@ -90,20 +90,3 @@ CREATE TABLE transactions (
); );
ALTER TABLE users MODIFY COLUMN balance INT DEFAULT 1000; ALTER TABLE users MODIFY COLUMN balance INT DEFAULT 1000;
DROP TABLE IF EXISTS users;
CREATE TABLE users (
id INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) UNIQUE NOT NULL,
password VARCHAR(255) NOT NULL,
-- saldo yang dipakai oleh game.php
bank INT DEFAULT 1000,
-- saldo yang dipakai login.php (boleh dipakai atau hapus)
balance INT DEFAULT 1000,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
last_login TIMESTAMP NULL DEFAULT NULL
);