fear:revisi_login_php

This commit is contained in:
Yustina 2025-11-24 14:17:20 +07:00
parent 4030decced
commit 7469c53a90

View File

@ -1,45 +1,27 @@
<?php
//untuk halaman login
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
$query = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
$user = mysqli_fetch_assoc($query);
echo "firstname:".$row['firstname']."<br>";//kalo di file yang sama
echo "lastname:".$row['lastname']."<br>";
if (!$user) {
echo json_encode(["status" => "error", "msg" => "Username tidak ditemukan"]);
exit;
}
else echo "Username/password salah";
if ($user["password"] !== $password) {
echo json_encode(["status" => "error", "msg" => "Password salah"]);
exit;
}
echo json_encode([
"status" => "success",
"id" => $user["id"],
"username" => $user["username"],
"email" => $user["email"],
"role" => $user["role"]
]);
?>
<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>