| Server IP : 127.0.0.1 / Your IP : 192.168.99.1 Web Server : Apache/2.4.35 (Win64) OpenSSL/1.1.1b mod_fcgid/2.3.9 System : Windows NT WIN-JEJMB9EN0L0 10.0 build 14393 (Windows Server 2016) AMD64 User : Administrator ( 0) PHP Version : 7.4.33 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : OFF | Perl : OFF | Python : OFF | Sudo : OFF | Pkexec : OFF Directory : /bpkd/admin/includes/ |
Upload File : |
<?php
/**
* File: admin/includes/init.php
* Inisialisasi untuk halaman admin
*/
// =====================================================
// CEK APAKAH SESSION SUDAH AKTIF
// =====================================================
$session_active = (session_status() == PHP_SESSION_ACTIVE);
if (!$session_active) {
// Set session cookie parameters SEBELUM session_start()
session_set_cookie_params([
'lifetime' => 0,
'path' => '/',
'domain' => '',
'secure' => false, // Set true jika pakai HTTPS
'httponly' => true,
'samesite' => 'Strict'
]);
session_name('CMS_INSTANSI_SESSION');
session_start();
}
// =====================================================
// LOAD DATABASE
// =====================================================
// Load database (jika belum di-load)
if (!isset($conn)) {
require_once dirname(__DIR__) . '/config/database.php';
}
// =====================================================
// LOAD FUNGSI DARI FRONTEND
// =====================================================
// Load fungsi dari frontend init (HANYA SEKALI, tidak duplikat)
$frontend_init = dirname(__DIR__, 2) . '/includes/init.php';
if (file_exists($frontend_init)) {
require_once $frontend_init;
} else {
// Fallback jika frontend init tidak ada
if (!function_exists('sanitize')) {
function sanitize($input) {
global $conn;
if (!isset($conn)) {
require_once dirname(__DIR__) . '/config/database.php';
}
return mysqli_real_escape_string($conn, htmlspecialchars(trim($input), ENT_QUOTES, 'UTF-8'));
}
}
if (!function_exists('createSlug')) {
function createSlug($string) {
$string = strtolower($string);
$string = preg_replace('/[^a-z0-9\s-]/', '', $string);
$string = preg_replace('/\s+/', '-', $string);
$string = preg_replace('/-+/', '-', $string);
return trim($string, '-');
}
}
}
// =====================================================
// FUNGSI ADMIN
// =====================================================
/**
* Cek login untuk admin
*/
function isAdminLoggedIn() {
return isset($_SESSION['user_id']) && isset($_SESSION['role']);
}
/**
* Redirect jika belum login
*/
function requireLogin() {
if (!isAdminLoggedIn()) {
redirect('login.php');
}
}
/**
* Redirect jika bukan admin
*/
function requireAdmin() {
requireLogin();
if ($_SESSION['role'] != 'admin') {
redirect('dashboard.php');
}
}
/**
* Validasi ID
*/
function validateId($id) {
return isset($id) && is_numeric($id) && $id > 0;
}
/**
* Format tanggal
*/
function formatDate($date, $format = 'd M Y H:i') {
return date($format, strtotime($date));
}
/**
* Mendapatkan nama bulan
*/
function getMonthName($month) {
$months = [
1 => 'Januari', 'Februari', 'Maret', 'April', 'Mei', 'Juni',
'Juli', 'Agustus', 'September', 'Oktober', 'November', 'Desember'
];
return $months[(int)$month] ?? '';
}
/**
* Cek apakah URL adalah eksternal
* @param string $url URL yang akan dicek
* @return bool
*/
function isExternalUrl($url) {
return (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0);
}
if (!function_exists('redirect')) {
function redirect($url) {
header("Location: $url");
exit();
}
}
// Perbaiki fungsi isExternalUrl (ada syntax error di kode Anda)
if (!function_exists('isExternalUrl')) {
function isExternalUrl($url) {
return (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0);
}
}
?>