55 lines
1.6 KiB
PHP
55 lines
1.6 KiB
PHP
<?php
|
|
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>
|