fix: index.php and user.sql

This commit is contained in:
Matthew Florentino 2025-11-27 10:22:10 +07:00
parent b06b6a4160
commit bfe36fbd4f
2 changed files with 42 additions and 34 deletions

View File

@ -1,45 +1,44 @@
<?php <?php
session_start(); session_start();
include "config/db.php"; include "config/db.php";
include "notif.php";
if(isset($_POST['login'])){ if (isset($_POST['login'])) {
$username = $_POST['username']; $username = trim($_POST['username']);
$password = $_POST['password']; $password = trim($_POST['password']);
if(empty($username)||empty($password)){ if ($username === "" || $password === "") {
$msg = "Data Harus Lengkap !"; $_SESSION['flash'] = "Data harus lengkap!";
echo "<script>showNotif(" . json_encode($msg) . ");</script>"; }
}else{ $sql = "SELECT * FROM users WHERE username = ?";
$sql = "SELECT * FROM users WHERE username =?"; $stmt = $db->prepare($sql); // agar aman dari sql injection
$stmt = $db -> prepare($sql); $stmt->bind_param("s", $username); // agar rapi tidak muncul di bagian atas query
$stmt->bind_param('s',$username);
$stmt->execute(); $stmt->execute();
$result = $stmt ->get_result(); $result = $stmt->get_result();
if($result->num_rows ===1){ if ($result->num_rows === 1) {
$user = $result ->fetch_assoc();
if(password_verify($password,$user['password'])){ $user = $result->fetch_assoc();
$_SESSION['loggedin']='true';
$_SESSION['username']=$user['username']; if (password_verify($password, $user['password'])) {
$_SESSION['id']=$user['id']; $_SESSION['loggedin'] = true;
header("location:onboard.php"); $_SESSION['username'] = $user['username'];
$_SESSION['id'] = $user['id'];
$_SESSION['flash'] = "Wellcome to Dungeon, player $username";
header("Location: onboard.php");
exit(); exit();
}else{
$msg = "Password Salah !";
echo "<script>showNotif(" . json_encode($msg) . ");</script>";
}
}else{
$msg = "Username tidak ditemukan !";
echo "<script>showNotif(" . json_encode($msg) . ");</script>";
}
$stmt->close();
}
}
$db->close();
} else {
$_SESSION['flash'] = "Password salah!";
}
} else {
$_SESSION['flash'] = "Username tidak ditemukan!";
}
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
<html lang="en"> <html lang="en">
<head> <head>
@ -51,14 +50,23 @@ $db->close();
<link rel="stylesheet" href="/css/style.css"> <link rel="stylesheet" href="/css/style.css">
</head> </head>
<body> <body>
<div class="container-login"> <div class="container-login">
<?php
include "notif.php";
if (isset($_SESSION['flash'])) {
echo "<script>showNotif(" . json_encode($_SESSION['flash']) . ");</script>";
unset($_SESSION['flash']);
}
?>
<h1 data-aos="zoom-out" data-aos-duration="1000">codebeater</h1> <h1 data-aos="zoom-out" data-aos-duration="1000">codebeater</h1>
<form class="login-form" action ="index.php" method="POST"> <form class="login-form" action ="index.php" method="POST">
<h2 data-aos="fade-up" data-aos-duration="1000">Login to your account</h2> <h2 data-aos="fade-up" data-aos-duration="1000">Login to your account</h2>
<input type="text" name="username" id="username" placeholder="input your username" data-aos="fade-up" data-aos-duration="2000"><br> <input type="text" name="username" id="username" placeholder="input your username" data-aos="fade-up" data-aos-duration="2000"><br>
<input type="password" name="password" id="password" placeholder="input your password" data-aos="fade-up" data-aos-duration="2000"><br> <input type="password" name="password" id="password" placeholder="input your password" data-aos="fade-up" data-aos-duration="2000"><br>
<button type="submit" name="login" placeholder="input your password" data-aos="fade-up" data-aos-duration="3000">LOGIN</button> <button type="submit" name="login" data-aos="fade-up" data-aos-duration="3000">LOGIN</button>
<p placeholder="input your password" data-aos="fade-up" data-aos-duration="3000"><a href="register.php">don't have account yet?<span> register now!</span></a></p> <p data-aos="fade-up" data-aos-duration="3000"><a href="register.php">don't have account yet?<span> register now!</span></a></p>
</form> </form>
<div id="notif" class="notif"></div> <div id="notif" class="notif"></div>
</div> </div>