connect_error) { http_response_code(500); die(json_encode([ 'status' => 'error', 'message' => 'Koneksi database gagal: ' . $conn->connect_error ])); } $conn->set_charset('utf8mb4'); function getRedirectPath($targetFile) { $currentDir = basename(dirname($_SERVER['SCRIPT_FILENAME'])); if (stripos($currentDir, 'Tugas') !== false || stripos($currentDir, 'tugas') !== false || $currentDir === 'includes') { return '../' . $targetFile; } return $targetFile; } function cekLogin() { if (session_status() === PHP_SESSION_NONE) { session_start(); } if (!isset($_SESSION['user_id'])) { header('Location: ' . getRedirectPath('login.php')); exit; } } function cekRole($role_wajib) { if (session_status() === PHP_SESSION_NONE) { session_start(); } if (!isset($_SESSION['role']) || $_SESSION['role'] !== $role_wajib) { header('Location: ' . getRedirectPath('dashboard.php?error=unauthorized')); exit; } } function bersih($str, $conn) { return $conn->real_escape_string(htmlspecialchars(strip_tags(trim($str)))); } ?>