Login and register integrated with database, commencing trial
This commit is contained in:
parent
a4510dccb1
commit
03c8e05c8d
@ -2,7 +2,7 @@
|
|||||||
$host = "localhost";
|
$host = "localhost";
|
||||||
$user = "root"; // username DB
|
$user = "root"; // username DB
|
||||||
$pass = ""; // password DB
|
$pass = ""; // password DB
|
||||||
$db = "nama_database"; // ganti dengan nama database kamu
|
$db = "game_db";
|
||||||
|
|
||||||
$conn = mysqli_connect($host, $user, $pass, $db);
|
$conn = mysqli_connect($host, $user, $pass, $db);
|
||||||
|
|
||||||
|
|||||||
63
login.php
63
login.php
@ -1,45 +1,26 @@
|
|||||||
<?php
|
<?php
|
||||||
|
session_start();
|
||||||
//untuk halaman login
|
|
||||||
include "koneksi.php";
|
include "koneksi.php";
|
||||||
if (isset($_POST['username']))
|
|
||||||
{
|
$username = $_POST['username'];
|
||||||
$username=$_POST['username'];
|
$password = $_POST['password'];
|
||||||
}
|
|
||||||
else $username ='';
|
$enc = md5($password);
|
||||||
if (isset($_POST['password']))
|
|
||||||
{
|
$stmt = mysqli_prepare($conn, "SELECT * FROM user WHERE username=? AND password=?");
|
||||||
$password=$_POST['password'];
|
mysqli_stmt_bind_param($stmt, "ss", $username, $enc);
|
||||||
}
|
mysqli_stmt_execute($stmt);
|
||||||
else $password ='';
|
|
||||||
if ($username!='' || $password!='')
|
$result = mysqli_stmt_get_result($stmt);
|
||||||
{
|
if ($row = mysqli_fetch_assoc($result)) {
|
||||||
$stmt = mysqli_prepare($conn,"select * from user where username=? and password=?");
|
|
||||||
$enc=md5($password);
|
$_SESSION['username'] = $row['username'];
|
||||||
mysqli_stmt_bind_param($stmt,"ss", $username,$enc);
|
$_SESSION['email'] = $row['email'];
|
||||||
mysqli_stmt_execute($stmt);
|
|
||||||
|
header("Location: mainboard.html");
|
||||||
$result = mysqli_stmt_get_result($stmt);
|
exit;
|
||||||
if ($row=mysqli_fetch_assoc($result))
|
|
||||||
{
|
} else {
|
||||||
echo "Login sukses";
|
echo "<script>alert('Username atau password salah!'); window.history.back();</script>";
|
||||||
$_session['firstname'] = $row['firstname']; //simpan session firstname
|
|
||||||
$_session['lastname'] = $row['lastname'];
|
|
||||||
header("Location:dashboard.php"); //untuk pindah ke halaman dashboard.php
|
|
||||||
|
|
||||||
echo "firstname:".$row['firstname']."<br>";//kalo di file yang sama
|
|
||||||
echo "lastname:".$row['lastname']."<br>";
|
|
||||||
}
|
|
||||||
else echo "Username/password salah";
|
|
||||||
}
|
}
|
||||||
?>
|
?>
|
||||||
|
|
||||||
<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>
|
|
||||||
74
register.php
74
register.php
@ -1,55 +1,29 @@
|
|||||||
<?php
|
<?php
|
||||||
if ($_SERVER["REQUEST_METHOD"] === "POST") {
|
include "koneksi.php";
|
||||||
|
|
||||||
$username = trim($_POST["username"]);
|
$username = $_POST['username'];
|
||||||
$email = trim($_POST["email"]);
|
$email = $_POST['email'];
|
||||||
$password = $_POST["password"];
|
$password = $_POST['password'];
|
||||||
$confirm = $_POST["confirm"];
|
$confirm = $_POST['confirm_password'];
|
||||||
|
|
||||||
if (!$username || !$email || !$password || !$confirm) {
|
if ($password != $confirm) {
|
||||||
echo "Semua field wajib diisi";
|
echo "<script>alert('Password tidak sama'); window.history.back();</script>";
|
||||||
exit;
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
?>
|
?>
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user