Login and register integrated with database, commencing trial

This commit is contained in:
Nathan 2025-12-09 14:49:33 +07:00
parent a4510dccb1
commit 03c8e05c8d
3 changed files with 47 additions and 92 deletions

View File

@ -2,7 +2,7 @@
$host = "localhost";
$user = "root"; // username DB
$pass = ""; // password DB
$db = "nama_database"; // ganti dengan nama database kamu
$db = "game_db";
$conn = mysqli_connect($host, $user, $pass, $db);

View File

@ -1,45 +1,26 @@
<?php
//untuk halaman login
session_start();
include "koneksi.php";
if (isset($_POST['username']))
{
$username=$_POST['username'];
}
else $username ='';
if (isset($_POST['password']))
{
$password=$_POST['password'];
}
else $password ='';
if ($username!='' || $password!='')
{
$stmt = mysqli_prepare($conn,"select * from user where username=? and password=?");
$enc=md5($password);
mysqli_stmt_bind_param($stmt,"ss", $username,$enc);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row=mysqli_fetch_assoc($result))
{
echo "Login sukses";
$_session['firstname'] = $row['firstname']; //simpan session firstname
$_session['lastname'] = $row['lastname'];
header("Location:dashboard.php"); //untuk pindah ke halaman dashboard.php
$username = $_POST['username'];
$password = $_POST['password'];
echo "firstname:".$row['firstname']."<br>";//kalo di file yang sama
echo "lastname:".$row['lastname']."<br>";
}
else echo "Username/password salah";
$enc = md5($password);
$stmt = mysqli_prepare($conn, "SELECT * FROM user WHERE username=? AND password=?");
mysqli_stmt_bind_param($stmt, "ss", $username, $enc);
mysqli_stmt_execute($stmt);
$result = mysqli_stmt_get_result($stmt);
if ($row = mysqli_fetch_assoc($result)) {
$_SESSION['username'] = $row['username'];
$_SESSION['email'] = $row['email'];
header("Location: mainboard.html");
exit;
} else {
echo "<script>alert('Username atau password salah!'); window.history.back();</script>";
}
?>
<html>
<body>
<form method="POST">
Username: <input type="text" name="username"><br>
Password:<input type="password" name="password"><br>
<input type="submit">
</form>
</body>
</html>

View File

@ -1,55 +1,29 @@
<?php
if ($_SERVER["REQUEST_METHOD"] === "POST") {
include "koneksi.php";
$username = trim($_POST["username"]);
$email = trim($_POST["email"]);
$password = $_POST["password"];
$confirm = $_POST["confirm"];
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$confirm = $_POST['confirm_password'];
if (!$username || !$email || !$password || !$confirm) {
echo "Semua field wajib diisi";
if ($password != $confirm) {
echo "<script>alert('Password tidak sama'); window.history.back();</script>";
exit;
}
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
echo "Email tidak valid";
exit;
}
if ($password !== $confirm) {
echo "Password tidak cocok";
exit;
}
$file = "users.json";
$users = file_exists($file) ? json_decode(file_get_contents($file), true) : [];
// 🔍 Cek username & email duplikat
foreach ($users as $u) {
if (strtolower($u["username"]) === strtolower($username)) {
echo "Username sudah digunakan";
exit;
}
if (strtolower($u["email"]) === strtolower($email)) {
echo "Email sudah digunakan";
exit;
}
}
// 🔐 Enkripsi password
$hashedPassword = password_hash($password, PASSWORD_BCRYPT);
// 📌 tambah user baru
$users[] = [
"id" => uniqid(),
"username" => $username,
"email" => $email,
"password" => $hashedPassword,
"role" => "player",
"created_at" => date("Y-m-d H:i:s")
];
file_put_contents($file, json_encode($users, JSON_PRETTY_PRINT));
echo "success";
}
$cek = mysqli_query($conn, "SELECT * FROM user WHERE username='$username' OR email='$email'");
if (mysqli_num_rows($cek) > 0) {
echo "<script>alert('Username atau email sudah digunakan'); window.history.back();</script>";
exit;
}
$hash = md5($password);
$sql = "INSERT INTO user (username, email, password) VALUES ('$username', '$email', '$hash')";
if (mysqli_query($conn, $sql)) {
echo "<script>alert('Registrasi berhasil! Silakan login.'); window.location='login.html';</script>";
} else {
echo "Error: " . mysqli_error($conn);
}
?>