85 lines
2.3 KiB
PHP
85 lines
2.3 KiB
PHP
<?php
|
|
<<<<<<< HEAD
|
|
session_start();
|
|
|
|
// untuk halaman login
|
|
include "koneksi.php";
|
|
|
|
$username = isset($_POST['username']) ? $_POST['username'] : '';
|
|
$password = isset($_POST['password']) ? $_POST['password'] : '';
|
|
|
|
// Pastikan kedua field terisi
|
|
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)) {
|
|
// Simpan session dengan nama yang benar
|
|
$_SESSION['firstname'] = $row['firstname'];
|
|
$_SESSION['lastname'] = $row['lastname'];
|
|
|
|
// Jika request AJAX, kembalikan JSON, jika bukan, redirect ke dashboard
|
|
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
|
if ($isAjax) {
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['success' => true, 'message' => 'Login sukses']);
|
|
exit;
|
|
} else {
|
|
header("Location: dashboard.php");
|
|
exit;
|
|
}
|
|
} else {
|
|
$isAjax = !empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
|
|
if ($isAjax) {
|
|
header('Content-Type: application/json');
|
|
echo json_encode(['success' => false, 'message' => 'Username/password salah']);
|
|
exit;
|
|
} else {
|
|
echo "Username/password salah";
|
|
}
|
|
}
|
|
}
|
|
?>
|
|
|
|
<!doctype html>
|
|
<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>
|
|
=======
|
|
include "koneksi.php";
|
|
|
|
$username = $_POST['username'];
|
|
$password = $_POST['password'];
|
|
|
|
$query = mysqli_query($conn, "SELECT * FROM users WHERE username='$username'");
|
|
$user = mysqli_fetch_assoc($query);
|
|
|
|
if (!$user) {
|
|
echo json_encode(["status" => "error", "msg" => "Username tidak ditemukan"]);
|
|
exit;
|
|
}
|
|
|
|
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"]
|
|
]);
|
|
?>
|
|
>>>>>>> 034e71546358d1709edc3df62da8342c7a9fa647
|