55 lines
1.5 KiB
PHP
55 lines
1.5 KiB
PHP
<?php
|
|
// config.php
|
|
if (session_status() === PHP_SESSION_NONE) {
|
|
session_start();
|
|
}
|
|
|
|
// Mengizinkan semua origin lokal saat masa development agar cookie session tidak diblokir
|
|
header('Content-Type: application/json');
|
|
header('Access-Control-Allow-Origin: ' . ($_SERVER['HTTP_ORIGIN'] ?? '*'));
|
|
header('Access-Control-Allow-Credentials: true');
|
|
header('Access-Control-Allow-Methods: GET, POST, PUT, DELETE, OPTIONS');
|
|
header('Access-Control-Allow-Headers: Content-Type, Access-Control-Allow-Headers, Authorization, X-Requested-With');
|
|
|
|
if ($_SERVER['REQUEST_METHOD'] === 'OPTIONS') {
|
|
http_response_code(200);
|
|
exit();
|
|
}
|
|
|
|
$host = 'localhost';
|
|
$username = 'root';
|
|
$password = '';
|
|
$database = 'gis_management';
|
|
$port = 3307; // Sesuai port database XAMPP Anda
|
|
|
|
$conn = new mysqli($host, $username, $password, $database, $port);
|
|
|
|
if ($conn->connect_error) {
|
|
die(json_encode([
|
|
'success' => false,
|
|
'message' => 'Koneksi database gagal: ' . $conn->connect_error
|
|
]));
|
|
}
|
|
|
|
$conn->set_charset("utf8");
|
|
|
|
// Fungsi standar untuk mengirim respon JSON
|
|
function sendResponse($success, $message, $data = null) {
|
|
$response = [
|
|
'success' => $success,
|
|
'message' => $message
|
|
];
|
|
if ($data !== null) {
|
|
$response['data'] = $data;
|
|
}
|
|
echo json_encode($response);
|
|
exit;
|
|
}
|
|
|
|
// Fungsi bantu untuk mengecek apakah user sudah login
|
|
function checkLogin() {
|
|
if (!isset($_SESSION['user_id'])) {
|
|
sendResponse(false, 'Status tidak terautentikasi. Silakan login terlebih dahulu.');
|
|
}
|
|
}
|
|
?>
|