perbaikan
This commit is contained in:
parent
0275422dd0
commit
a3aaaa3dca
16
db.php
16
db.php
@ -1,20 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
$host = 'localhost';
|
$host = 'localhost';
|
||||||
$db = 'sudoku';
|
|
||||||
$user = 'root';
|
$user = 'root';
|
||||||
$pass = '';
|
$pass = '';
|
||||||
$charset = 'utf8mb4';
|
$db = 'sudoku';
|
||||||
|
|
||||||
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
$conn = mysqli_connect($host, $user, $pass, $db);
|
||||||
$options = [
|
|
||||||
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
|
||||||
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
|
||||||
PDO::ATTR_EMULATE_PREPARES => false,
|
|
||||||
];
|
|
||||||
|
|
||||||
try {
|
if (!$conn) {
|
||||||
$conn = new PDO($dsn, $user, $pass, $options);
|
die("Connection failed: " . mysqli_connect_error());
|
||||||
} catch (\PDOException $e) {
|
|
||||||
throw new \PDOException($e->getMessage(), (int)$e->getCode());
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@ -2,20 +2,18 @@
|
|||||||
require_once 'db.php';
|
require_once 'db.php';
|
||||||
|
|
||||||
|
|
||||||
try {
|
// Check table existence (using MySQLi)
|
||||||
$checkTable = $conn->query("SHOW TABLES LIKE 'leaderboard_sudoku'");
|
$checkTable = mysqli_query($conn, "SHOW TABLES LIKE 'leaderboard_sudoku'");
|
||||||
if ($checkTable->rowCount() == 0) {
|
if (mysqli_num_rows($checkTable) == 0) {
|
||||||
$createSql = "
|
$createSql = "
|
||||||
CREATE TABLE leaderboard_sudoku (
|
CREATE TABLE leaderboard_sudoku (
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
username VARCHAR(50),
|
username VARCHAR(50),
|
||||||
difficulty VARCHAR(10),
|
difficulty VARCHAR(10),
|
||||||
time_seconds INT,
|
time_seconds INT,
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
)";
|
)";
|
||||||
$conn->exec($createSql);
|
mysqli_query($conn, $createSql);
|
||||||
}
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLeaderboard($conn, $difficulty) {
|
function getLeaderboard($conn, $difficulty) {
|
||||||
@ -27,13 +25,21 @@ function getLeaderboard($conn, $difficulty) {
|
|||||||
$sql = "
|
$sql = "
|
||||||
SELECT username, time_seconds, created_at
|
SELECT username, time_seconds, created_at
|
||||||
FROM leaderboard_sudoku
|
FROM leaderboard_sudoku
|
||||||
WHERE difficulty = :difficulty
|
WHERE difficulty = ?
|
||||||
ORDER BY time_seconds ASC
|
ORDER BY time_seconds ASC
|
||||||
LIMIT 10
|
LIMIT 10
|
||||||
";
|
";
|
||||||
$stmt = $conn->prepare($sql);
|
|
||||||
$stmt->execute(['difficulty' => $difficulty]);
|
$stmt = mysqli_prepare($conn, $sql);
|
||||||
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
mysqli_stmt_bind_param($stmt, "s", $difficulty);
|
||||||
|
mysqli_stmt_execute($stmt);
|
||||||
|
$result = mysqli_stmt_get_result($stmt);
|
||||||
|
|
||||||
|
$rows = [];
|
||||||
|
while ($row = mysqli_fetch_assoc($result)) {
|
||||||
|
$rows[] = $row;
|
||||||
|
}
|
||||||
|
return $rows;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isset($_GET['api']) && isset($_GET['difficulty'])) {
|
if (isset($_GET['api']) && isset($_GET['difficulty'])) {
|
||||||
|
|||||||
44
login.php
44
login.php
@ -16,36 +16,31 @@ if (isset($_POST['login'])) {
|
|||||||
if ($username_input === '' || $pass === '') {
|
if ($username_input === '' || $pass === '') {
|
||||||
$err = "Username dan password harus diisi.";
|
$err = "Username dan password harus diisi.";
|
||||||
} else {
|
} else {
|
||||||
try {
|
// 1. Ambil data user berdasarkan username
|
||||||
// 1. Ambil data user berdasarkan username
|
// Menggunakan Prepared Statement MySQLi
|
||||||
// Menggunakan Prepared Statement
|
$stmt = mysqli_prepare($conn, "SELECT id, username, password FROM users WHERE username = ?");
|
||||||
$stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ?");
|
mysqli_stmt_bind_param($stmt, "s", $username_input);
|
||||||
$stmt->execute([$username_input]);
|
mysqli_stmt_execute($stmt);
|
||||||
$user_data = $stmt->fetch(PDO::FETCH_ASSOC);
|
$result = mysqli_stmt_get_result($stmt);
|
||||||
|
$user_data = mysqli_fetch_assoc($result);
|
||||||
|
|
||||||
// 2. Verifikasi Password
|
// 2. Verifikasi Password
|
||||||
// password_verify akan mencocokkan input user dengan HASH di database
|
if ($user_data && password_verify($pass, $user_data['password'])) {
|
||||||
if ($user_data && password_verify($pass, $user_data['password'])) {
|
|
||||||
|
|
||||||
// Regenerasi ID Session
|
session_regenerate_id(true);
|
||||||
session_regenerate_id(true);
|
|
||||||
|
|
||||||
// Simpan data ke session
|
$_SESSION['user_id'] = $user_data['id'];
|
||||||
$_SESSION['user_id'] = $user_data['id'];
|
$_SESSION['username'] = $user_data['username'];
|
||||||
$_SESSION['username'] = $user_data['username'];
|
$_SESSION['login'] = true;
|
||||||
$_SESSION['login'] = true;
|
|
||||||
|
|
||||||
// Arahkan ke halaman game
|
header("Location: sudoku.php");
|
||||||
header("Location: sudoku.php");
|
exit();
|
||||||
exit();
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
$err = "Username atau password salah.";
|
$err = "Username atau password salah.";
|
||||||
}
|
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
$err = "Terjadi kesalahan sistem database.";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@ -53,7 +48,6 @@ if (isset($_POST['login'])) {
|
|||||||
<head>
|
<head>
|
||||||
<title>Login</title>
|
<title>Login</title>
|
||||||
<style>
|
<style>
|
||||||
/* Style disamakan persis dengan register.php Anda */
|
|
||||||
body { font-family: Arial; background:aliceblue; display:flex; height:100vh; justify-content:center; align-items:center; margin:0; }
|
body { font-family: Arial; background:aliceblue; display:flex; height:100vh; justify-content:center; align-items:center; margin:0; }
|
||||||
.card { width:350px; background:white; padding:20px; border-radius:10px; box-shadow:0 6px 20px rgba(0,0,0,0.1); }
|
.card { width:350px; background:white; padding:20px; border-radius:10px; box-shadow:0 6px 20px rgba(0,0,0,0.1); }
|
||||||
.input { width:100%; padding:10px; margin:8px 0; border:1px solid lightgray; border-radius:8px; box-sizing: border-box; }
|
.input { width:100%; padding:10px; margin:8px 0; border:1px solid lightgray; border-radius:8px; box-sizing: border-box; }
|
||||||
|
|||||||
29
register.php
29
register.php
@ -25,25 +25,30 @@ if (isset($_POST['register'])) {
|
|||||||
$err = "Password terlalu pendek. Minimal 6 karakter.";
|
$err = "Password terlalu pendek. Minimal 6 karakter.";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
try {
|
// 1. Check Username
|
||||||
$check = $conn->prepare("SELECT id FROM users WHERE username = ?");
|
$check = mysqli_prepare($conn, "SELECT id FROM users WHERE username = ?");
|
||||||
$check->execute([$username_input]);
|
mysqli_stmt_bind_param($check, "s", $username_input);
|
||||||
|
mysqli_stmt_execute($check);
|
||||||
|
mysqli_stmt_store_result($check);
|
||||||
|
|
||||||
if($check->rowCount() > 0){
|
if(mysqli_stmt_num_rows($check) > 0){
|
||||||
$err = "Username sudah terdaftar, silakan pilih nama lain.";
|
$err = "Username sudah terdaftar, silakan pilih nama lain.";
|
||||||
} else {
|
} else {
|
||||||
$hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
|
$hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
// 2. Insert User
|
||||||
$stmt->execute([$username_input, $hashed_pass]);
|
$stmt = mysqli_prepare($conn, "INSERT INTO users (username, password) VALUES (?, ?)");
|
||||||
|
mysqli_stmt_bind_param($stmt, "ss", $username_input, $hashed_pass);
|
||||||
|
|
||||||
|
if (mysqli_stmt_execute($stmt)) {
|
||||||
$success = "Registrasi berhasil! Silakan <a href='login.php'>Login disini</a>.";
|
$success = "Registrasi berhasil! Silakan <a href='login.php'>Login disini</a>.";
|
||||||
|
|
||||||
$username_input = '';
|
$username_input = '';
|
||||||
|
} else {
|
||||||
|
$err = "Gagal mendaftar: " . mysqli_error($conn);
|
||||||
}
|
}
|
||||||
} catch (PDOException $e) {
|
mysqli_stmt_close($stmt);
|
||||||
$err = "Terjadi kesalahan sistem: " . $e->getMessage();
|
|
||||||
}
|
}
|
||||||
|
mysqli_stmt_close($check);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@ -16,15 +16,16 @@ $username = $_SESSION['username'];
|
|||||||
$difficulty = $_POST['difficulty'];
|
$difficulty = $_POST['difficulty'];
|
||||||
$time = (int) $_POST['time'];
|
$time = (int) $_POST['time'];
|
||||||
|
|
||||||
$sql = "INSERT INTO leaderboard_sudoku
|
$sql = "INSERT INTO leaderboard_sudoku (username, difficulty, time_seconds) VALUES (?, ?, ?)";
|
||||||
(username, difficulty, time_seconds)
|
|
||||||
VALUES (:username, :difficulty, :time)";
|
|
||||||
|
|
||||||
$stmt = $conn->prepare($sql);
|
$stmt = mysqli_prepare($conn, $sql);
|
||||||
$stmt->execute([
|
mysqli_stmt_bind_param($stmt, "ssi", $username, $difficulty, $time);
|
||||||
':username' => $username,
|
|
||||||
':difficulty' => $difficulty,
|
if (mysqli_stmt_execute($stmt)) {
|
||||||
':time' => $time
|
echo "SUCCESS";
|
||||||
]);
|
} else {
|
||||||
|
echo "ERROR: " . mysqli_error($conn);
|
||||||
|
}
|
||||||
|
mysqli_stmt_close($stmt);
|
||||||
|
|
||||||
echo "SUCCESS";
|
echo "SUCCESS";
|
||||||
89
setup.php
89
setup.php
@ -2,72 +2,67 @@
|
|||||||
$host = 'localhost';
|
$host = 'localhost';
|
||||||
$user = 'root';
|
$user = 'root';
|
||||||
$pass = ''; // Default XAMPP password
|
$pass = ''; // Default XAMPP password
|
||||||
$charset = 'utf8mb4';
|
$db = 'sudoku';
|
||||||
|
|
||||||
// 1. Connect to MySQL server (without DB) to create database
|
// 1. Connect to MySQL server (without DB) to create database
|
||||||
try {
|
$conn = mysqli_connect($host, $user, $pass);
|
||||||
$pdo = new PDO("mysql:host=$host;charset=$charset", $user, $pass);
|
|
||||||
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
|
|
||||||
// Create Database if not exists
|
if (!$conn) {
|
||||||
$pdo->exec("CREATE DATABASE IF NOT EXISTS sudoku CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci");
|
die("Connection failed: " . mysqli_connect_error());
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create Database if not exists
|
||||||
|
$sql = "CREATE DATABASE IF NOT EXISTS sudoku CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci";
|
||||||
|
if (mysqli_query($conn, $sql)) {
|
||||||
echo "Database 'sudoku' successfully checked/created.<br>";
|
echo "Database 'sudoku' successfully checked/created.<br>";
|
||||||
} catch (PDOException $e) {
|
} else {
|
||||||
die("DB Connection failed: " . $e->getMessage());
|
echo "Error creating database: " . mysqli_error($conn) . "<br>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 2. Connect to the specific Database 'sudoku'
|
// 2. Connect to the specific Database 'sudoku'
|
||||||
try {
|
mysqli_select_db($conn, $db);
|
||||||
$dsn = "mysql:host=$host;dbname=sudoku;charset=$charset";
|
|
||||||
$conn = new PDO($dsn, $user, $pass);
|
|
||||||
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
||||||
} catch (PDOException $e) {
|
|
||||||
die("Connection to 'sudoku' failed: " . $e->getMessage());
|
|
||||||
}
|
|
||||||
|
|
||||||
// 3. Create 'users' table
|
// 3. Create 'users' table
|
||||||
try {
|
$sqlUsers = "
|
||||||
$sqlUsers = "
|
CREATE TABLE IF NOT EXISTS users (
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
username VARCHAR(50) NOT NULL UNIQUE,
|
||||||
username VARCHAR(50) NOT NULL UNIQUE,
|
password VARCHAR(255) NOT NULL
|
||||||
password VARCHAR(255) NOT NULL
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
|
||||||
|
|
||||||
$conn->exec($sqlUsers);
|
if (mysqli_query($conn, $sqlUsers)) {
|
||||||
echo "Table 'users' successfully checked/created.<br>";
|
echo "Table 'users' successfully checked/created.<br>";
|
||||||
|
|
||||||
// Seed default users if table is empty
|
// Seed default users if table is empty
|
||||||
$check = $conn->query("SELECT count(*) FROM users")->fetchColumn();
|
$checkResult = mysqli_query($conn, "SELECT count(*) as total FROM users");
|
||||||
if ($check == 0) {
|
$row = mysqli_fetch_assoc($checkResult);
|
||||||
$password = password_hash('123456', PASSWORD_DEFAULT); // Default password
|
|
||||||
$sqlInsert = "INSERT INTO users (username, password) VALUES
|
|
||||||
('admin', '$password'),
|
|
||||||
('player1', '$password')";
|
|
||||||
$conn->exec($sqlInsert);
|
|
||||||
echo "Default users (admin, player1) created with password '123456'.<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
} catch (PDOException $e) {
|
if ($row['total'] == 0) {
|
||||||
echo "Error Creating 'users': " . $e->getMessage() . "<br>";
|
$password = password_hash('123456', PASSWORD_DEFAULT); // Default password
|
||||||
|
// Using prepare statement for insertion to be safe, though not strictly necessary for hardcoded
|
||||||
|
$sqlInsert = "INSERT INTO users (username, password) VALUES ('admin', '$password'), ('player1', '$password')";
|
||||||
|
if (mysqli_query($conn, $sqlInsert)) {
|
||||||
|
echo "Default users (admin, player1) created with password '123456'.<br>";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
echo "Error Creating 'users': " . mysqli_error($conn) . "<br>";
|
||||||
}
|
}
|
||||||
|
|
||||||
// 4. Create 'leaderboard_sudoku' table
|
// 4. Create 'leaderboard_sudoku' table
|
||||||
try {
|
$sqlLeaderboard = "
|
||||||
$sqlLeaderboard = "
|
CREATE TABLE IF NOT EXISTS leaderboard_sudoku (
|
||||||
CREATE TABLE IF NOT EXISTS leaderboard_sudoku (
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
username VARCHAR(50),
|
||||||
username VARCHAR(50),
|
difficulty VARCHAR(10),
|
||||||
difficulty VARCHAR(10),
|
time_seconds INT,
|
||||||
time_seconds INT,
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
)";
|
||||||
)";
|
|
||||||
|
|
||||||
$conn->exec($sqlLeaderboard);
|
if (mysqli_query($conn, $sqlLeaderboard)) {
|
||||||
echo "Table 'leaderboard_sudoku' successfully checked/created.<br>";
|
echo "Table 'leaderboard_sudoku' successfully checked/created.<br>";
|
||||||
|
} else {
|
||||||
} catch (PDOException $e) {
|
echo "Error Creating 'leaderboard_sudoku': " . mysqli_error($conn) . "<br>";
|
||||||
echo "Error Creating 'leaderboard_sudoku': " . $e->getMessage() . "<br>";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
echo "<hr><strong>Setup Complete!</strong> You can now use the application.";
|
echo "<hr><strong>Setup Complete!</strong> You can now use the application.";
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user