Compare commits

...

2 Commits

Author SHA1 Message Date
ody
a8901fb771 saldo save sql 2025-12-03 18:30:40 +07:00
ody
13c0a8a7dd ubah fix 2025-12-03 18:28:08 +07:00
2 changed files with 164 additions and 110 deletions

View File

@ -1,141 +1,178 @@
<?php
include 'koneksi.php';
include "koneksi.php";
session_start();
$success = false;
/* ==========================================================
======================= LOGIN ============================
========================================================== */
if (isset($_POST['register'])) {
$username = $_POST['username'];
$error = "";
if (isset($_POST['login'])) {
$username = mysqli_real_escape_string($conn, $_POST['username']);
$password = $_POST['password'];
// basic escaping to avoid simple injection (keep consistent with existing style)
$username = mysqli_real_escape_string($conn, $username);
$password = mysqli_real_escape_string($conn, $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);
// insert with initial balance = 0
$SQL = "INSERT INTO users (username, password, balance) VALUES ('$username', '$password', 0)";
$result = mysqli_query($conn, $SQL);
if (mysqli_num_rows($result) > 0) {
$user = mysqli_fetch_assoc($result);
if ($result) {
$success = true;
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 lang="en">
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>OCA Gaming Hub - Login</title>
<title>Login / Register</title>
<link rel="stylesheet" href="login.css">
</head>
<body>
<div class="container">
<div class="container">
<div class="logo">
<h1> OCA GAMING HUB </h1>
<p>BLACKJACK 21 CARD GAME</p>
<h1>OCAGamingHub</h1>
<p>Sign in or create account</p>
</div>
<div class="form-container">
<div class="card-icon">🂡</div>
<?php if ($success): ?>
<div class="success-message show">Register Success!</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 if (!empty($error)): ?>
<div class="error-message show"><?= htmlspecialchars($error) ?></div>
<?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>
<script>
function goToMain() {
document.getElementById('mainPage').style.display = 'block';
document.getElementById('signupForm').style.display = 'none';
// Clear messages
document.getElementById('mainMessage').classList.remove('show');
document.getElementById('mainError').classList.remove('show');
}
<div class="form-group">
<label>Password</label>
<input type="password" name="password">
</div>
function goToSignUp() {
document.getElementById('mainPage').style.display = 'none';
document.getElementById('signupForm').style.display = 'block';
}
<button type="submit" name="login" class="btn btn-signin">Login</button>
</form>
// Login handler: validate input and show messages
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');
<hr>
// Reset messages
successEl.classList.remove('show');
errorEl.classList.remove('show');
<!-- ================== REGISTER FORM ================== -->
<form method="POST">
<h2>Register</h2>
if (!username || !password) {
errorEl.textContent = 'Please enter both username and password.';
errorEl.classList.add('show');
return;
}
<div class="form-group">
<label>Username</label>
<input type="text" name="username">
</div>
// 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');
}
}
<div class="form-group">
<label>Password</label>
<input type="password" name="password">
</div>
// 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;
<div class="form-group">
<label>Confirm Password</label>
<input type="password" name="confirm_password">
</div>
if (username && email && password) {
const message = document.getElementById('signupMessage');
message.textContent = `✓ Account created successfully for ${username}!`;
message.classList.add('show');
<button type="submit" name="register" class="btn btn-signup">Create Account</button>
</form>
setTimeout(() => {
alert(`Account created!\nUsername: ${username}\nEmail: ${email}`);
// Add your redirect here
goToMain();
}, 1500);
}
});
</script>
</div>
</div>
</body>
</html>

View File

@ -90,3 +90,20 @@ CREATE TABLE transactions (
);
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
);