55 lines
1.8 KiB
PHP
55 lines
1.8 KiB
PHP
<?php
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: *');
|
|
header('Access-Control-Allow-Methods: POST, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
$host = 'sql208.infinityfree.com';
|
|
$dbname = 'if0_42170546_sig_db';
|
|
$username = 'if0_42170546';
|
|
$password = 'SABTU01012005';
|
|
|
|
try {
|
|
$pdo = new PDO("mysql:host=$host;dbname=$dbname;charset=utf8", $username, $password);
|
|
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
|
|
} catch(PDOException $e) {
|
|
echo json_encode(['success' => false, 'message' => 'Database connection failed']);
|
|
exit();
|
|
}
|
|
|
|
$action = isset($_GET['action']) ? $_GET['action'] : '';
|
|
|
|
if ($action === 'login' && $_SERVER['REQUEST_METHOD'] === 'POST') {
|
|
$input = json_decode(file_get_contents('php://input'), true);
|
|
|
|
if (!$input || !isset($input['username']) || !isset($input['password'])) {
|
|
echo json_encode(['success' => false, 'message' => 'Username dan password harus diisi']);
|
|
exit();
|
|
}
|
|
|
|
$stmt = $pdo->prepare("SELECT * FROM users WHERE username = ? AND password = MD5(?)");
|
|
$stmt->execute([$input['username'], $input['password']]);
|
|
$user = $stmt->fetch(PDO::FETCH_ASSOC);
|
|
|
|
if ($user) {
|
|
echo json_encode([
|
|
'success' => true,
|
|
'message' => 'Login berhasil',
|
|
'data' => [
|
|
'id' => $user['id'],
|
|
'username' => $user['username'],
|
|
'role' => $user['role']
|
|
]
|
|
]);
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Username atau password salah']);
|
|
}
|
|
} else {
|
|
echo json_encode(['success' => false, 'message' => 'Invalid action']);
|
|
}
|
|
?>
|