Compare commits
No commits in common. "a3aaaa3dcaffe95423d6b9bfc5eea2576bb4ddb0" and "4f2f7ae38e73634b6fea52d45f4c049a68038486" have entirely different histories.
a3aaaa3dca
...
4f2f7ae38e
21
db.php
21
db.php
@ -1,12 +1,21 @@
|
|||||||
<?php
|
<?php
|
||||||
$host = 'localhost';
|
$host = 'localhost';
|
||||||
$user = 'root';
|
$db = 'sudoku'; // Sesuaikan dengan nama database Anda
|
||||||
$pass = '';
|
$user = 'root'; // Default user XAMPP
|
||||||
$db = 'sudoku';
|
$pass = ''; // Default password XAMPP (kosong)
|
||||||
|
$charset = 'utf8mb4';
|
||||||
|
|
||||||
$conn = mysqli_connect($host, $user, $pass, $db);
|
$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
|
||||||
|
$options = [
|
||||||
|
PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
|
||||||
|
PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
|
||||||
|
PDO::ATTR_EMULATE_PREPARES => false,
|
||||||
|
];
|
||||||
|
|
||||||
if (!$conn) {
|
try {
|
||||||
die("Connection failed: " . mysqli_connect_error());
|
// Membuat koneksi PDO
|
||||||
|
$conn = new PDO($dsn, $user, $pass, $options);
|
||||||
|
} catch (\PDOException $e) {
|
||||||
|
throw new \PDOException($e->getMessage(), (int)$e->getCode());
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@ -2,18 +2,20 @@
|
|||||||
require_once 'db.php';
|
require_once 'db.php';
|
||||||
|
|
||||||
|
|
||||||
// Check table existence (using MySQLi)
|
try {
|
||||||
$checkTable = mysqli_query($conn, "SHOW TABLES LIKE 'leaderboard_sudoku'");
|
$checkTable = $conn->query("SHOW TABLES LIKE 'leaderboard_sudoku'");
|
||||||
if (mysqli_num_rows($checkTable) == 0) {
|
if ($checkTable->rowCount() == 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
|
||||||
)";
|
)";
|
||||||
mysqli_query($conn, $createSql);
|
$conn->exec($createSql);
|
||||||
|
}
|
||||||
|
} catch (PDOException $e) {
|
||||||
}
|
}
|
||||||
|
|
||||||
function getLeaderboard($conn, $difficulty) {
|
function getLeaderboard($conn, $difficulty) {
|
||||||
@ -25,21 +27,13 @@ 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 = ?
|
WHERE difficulty = :difficulty
|
||||||
ORDER BY time_seconds ASC
|
ORDER BY time_seconds ASC
|
||||||
LIMIT 10
|
LIMIT 10
|
||||||
";
|
";
|
||||||
|
$stmt = $conn->prepare($sql);
|
||||||
$stmt = mysqli_prepare($conn, $sql);
|
$stmt->execute(['difficulty' => $difficulty]);
|
||||||
mysqli_stmt_bind_param($stmt, "s", $difficulty);
|
return $stmt->fetchAll(PDO::FETCH_ASSOC);
|
||||||
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,31 +16,36 @@ 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 {
|
||||||
// 1. Ambil data user berdasarkan username
|
try {
|
||||||
// Menggunakan Prepared Statement MySQLi
|
// 1. Ambil data user berdasarkan username
|
||||||
$stmt = mysqli_prepare($conn, "SELECT id, username, password FROM users WHERE username = ?");
|
// Menggunakan Prepared Statement
|
||||||
mysqli_stmt_bind_param($stmt, "s", $username_input);
|
$stmt = $conn->prepare("SELECT id, username, password FROM users WHERE username = ?");
|
||||||
mysqli_stmt_execute($stmt);
|
$stmt->execute([$username_input]);
|
||||||
$result = mysqli_stmt_get_result($stmt);
|
$user_data = $stmt->fetch(PDO::FETCH_ASSOC);
|
||||||
$user_data = mysqli_fetch_assoc($result);
|
|
||||||
|
|
||||||
// 2. Verifikasi Password
|
// 2. Verifikasi Password
|
||||||
if ($user_data && password_verify($pass, $user_data['password'])) {
|
// password_verify akan mencocokkan input user dengan HASH di database
|
||||||
|
if ($user_data && password_verify($pass, $user_data['password'])) {
|
||||||
|
|
||||||
session_regenerate_id(true);
|
// Regenerasi ID Session
|
||||||
|
session_regenerate_id(true);
|
||||||
|
|
||||||
$_SESSION['user_id'] = $user_data['id'];
|
// Simpan data ke session
|
||||||
$_SESSION['username'] = $user_data['username'];
|
$_SESSION['user_id'] = $user_data['id'];
|
||||||
$_SESSION['login'] = true;
|
$_SESSION['username'] = $user_data['username'];
|
||||||
|
$_SESSION['login'] = true;
|
||||||
|
|
||||||
header("Location: sudoku.php");
|
// Arahkan ke halaman game
|
||||||
exit();
|
header("Location: sudoku.php");
|
||||||
|
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);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
@ -48,6 +53,7 @@ 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; }
|
||||||
|
|||||||
35
register.php
35
register.php
@ -25,30 +25,25 @@ if (isset($_POST['register'])) {
|
|||||||
$err = "Password terlalu pendek. Minimal 6 karakter.";
|
$err = "Password terlalu pendek. Minimal 6 karakter.";
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
// 1. Check Username
|
try {
|
||||||
$check = mysqli_prepare($conn, "SELECT id FROM users WHERE username = ?");
|
$check = $conn->prepare("SELECT id FROM users WHERE username = ?");
|
||||||
mysqli_stmt_bind_param($check, "s", $username_input);
|
$check->execute([$username_input]);
|
||||||
mysqli_stmt_execute($check);
|
|
||||||
mysqli_stmt_store_result($check);
|
|
||||||
|
|
||||||
if(mysqli_stmt_num_rows($check) > 0){
|
if($check->rowCount() > 0){
|
||||||
$err = "Username sudah terdaftar, silakan pilih nama lain.";
|
$err = "Username sudah terdaftar, silakan pilih nama lain.";
|
||||||
} else {
|
|
||||||
$hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
|
|
||||||
|
|
||||||
// 2. Insert User
|
|
||||||
$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>.";
|
|
||||||
$username_input = '';
|
|
||||||
} else {
|
} else {
|
||||||
$err = "Gagal mendaftar: " . mysqli_error($conn);
|
$hashed_pass = password_hash($pass, PASSWORD_DEFAULT);
|
||||||
|
|
||||||
|
$stmt = $conn->prepare("INSERT INTO users (username, password) VALUES (?, ?)");
|
||||||
|
$stmt->execute([$username_input, $hashed_pass]);
|
||||||
|
|
||||||
|
$success = "Registrasi berhasil! Silakan <a href='login.php'>Login disini</a>.";
|
||||||
|
|
||||||
|
$username_input = '';
|
||||||
}
|
}
|
||||||
mysqli_stmt_close($stmt);
|
} catch (PDOException $e) {
|
||||||
|
$err = "Terjadi kesalahan sistem: " . $e->getMessage();
|
||||||
}
|
}
|
||||||
mysqli_stmt_close($check);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|||||||
@ -12,20 +12,19 @@ if (!isset($_POST['difficulty'], $_POST['time'])) {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
$username = $_SESSION['username'];
|
$username ='testuser';
|
||||||
$difficulty = $_POST['difficulty'];
|
$difficulty = $_POST['difficulty'];
|
||||||
$time = (int) $_POST['time'];
|
$time = (int) $_POST['time'];
|
||||||
|
|
||||||
$sql = "INSERT INTO leaderboard_sudoku (username, difficulty, time_seconds) VALUES (?, ?, ?)";
|
$sql = "INSERT INTO leaderboard_sudoku
|
||||||
|
(username, difficulty, time_seconds)
|
||||||
|
VALUES (:username, :difficulty, :time)";
|
||||||
|
|
||||||
$stmt = mysqli_prepare($conn, $sql);
|
$stmt = $conn->prepare($sql);
|
||||||
mysqli_stmt_bind_param($stmt, "ssi", $username, $difficulty, $time);
|
$stmt->execute([
|
||||||
|
':username' => $username,
|
||||||
if (mysqli_stmt_execute($stmt)) {
|
':difficulty' => $difficulty,
|
||||||
echo "SUCCESS";
|
':time' => $time
|
||||||
} else {
|
]);
|
||||||
echo "ERROR: " . mysqli_error($conn);
|
|
||||||
}
|
|
||||||
mysqli_stmt_close($stmt);
|
|
||||||
|
|
||||||
echo "SUCCESS";
|
echo "SUCCESS";
|
||||||
69
setup.php
69
setup.php
@ -1,69 +0,0 @@
|
|||||||
<?php
|
|
||||||
$host = 'localhost';
|
|
||||||
$user = 'root';
|
|
||||||
$pass = ''; // Default XAMPP password
|
|
||||||
$db = 'sudoku';
|
|
||||||
|
|
||||||
// 1. Connect to MySQL server (without DB) to create database
|
|
||||||
$conn = mysqli_connect($host, $user, $pass);
|
|
||||||
|
|
||||||
if (!$conn) {
|
|
||||||
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>";
|
|
||||||
} else {
|
|
||||||
echo "Error creating database: " . mysqli_error($conn) . "<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
// 2. Connect to the specific Database 'sudoku'
|
|
||||||
mysqli_select_db($conn, $db);
|
|
||||||
|
|
||||||
// 3. Create 'users' table
|
|
||||||
$sqlUsers = "
|
|
||||||
CREATE TABLE IF NOT EXISTS users (
|
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
username VARCHAR(50) NOT NULL UNIQUE,
|
|
||||||
password VARCHAR(255) NOT NULL
|
|
||||||
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
|
|
||||||
|
|
||||||
if (mysqli_query($conn, $sqlUsers)) {
|
|
||||||
echo "Table 'users' successfully checked/created.<br>";
|
|
||||||
|
|
||||||
// Seed default users if table is empty
|
|
||||||
$checkResult = mysqli_query($conn, "SELECT count(*) as total FROM users");
|
|
||||||
$row = mysqli_fetch_assoc($checkResult);
|
|
||||||
|
|
||||||
if ($row['total'] == 0) {
|
|
||||||
$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
|
|
||||||
$sqlLeaderboard = "
|
|
||||||
CREATE TABLE IF NOT EXISTS leaderboard_sudoku (
|
|
||||||
id INT AUTO_INCREMENT PRIMARY KEY,
|
|
||||||
username VARCHAR(50),
|
|
||||||
difficulty VARCHAR(10),
|
|
||||||
time_seconds INT,
|
|
||||||
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
|
|
||||||
)";
|
|
||||||
|
|
||||||
if (mysqli_query($conn, $sqlLeaderboard)) {
|
|
||||||
echo "Table 'leaderboard_sudoku' successfully checked/created.<br>";
|
|
||||||
} else {
|
|
||||||
echo "Error Creating 'leaderboard_sudoku': " . mysqli_error($conn) . "<br>";
|
|
||||||
}
|
|
||||||
|
|
||||||
echo "<hr><strong>Setup Complete!</strong> You can now use the application.";
|
|
||||||
?>
|
|
||||||
Loading…
x
Reference in New Issue
Block a user