<?php
/* ====================================================
SAFETY TALK SYSTEM - PT CITRA LAMPIA MANDIRI
File        : logout.php
Versi       : 1.0 (Session Termination)
Fitur       : - Destroy semua session
              - Clear remember-me cookie
              - Redirect ke login dengan pesan
              - Security: Prevent cache setelah logout
====================================================
*/

// Set timezone
date_default_timezone_set('Asia/Jakarta');

// Start session to access & destroy it
if(session_status() === PHP_SESSION_NONE) session_start();

// Store user name for farewell message (optional)
$userName = $_SESSION['user_name'] ?? 'Pengguna';

// Destroy all session variables
$_SESSION = array();

// Delete session cookie if exists
if(ini_get('session.use_cookies')) {
    $params = session_get_cookie_params();
    setcookie(session_name(), '', time() - 42000,
        $params['path'], $params['domain'],
        $params['secure'], $params['httponly']
    );
}

// Destroy the session
session_destroy();

// Clear remember-me cookie
if(isset($_COOKIE['remember_nama'])) {
    setcookie('remember_nama', '', time() - 3600, '/', '', false, true);
}

// Prevent caching of this page after logout
header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0');
header('Cache-Control: post-check=0, pre-check=0', false);
header('Pragma: no-cache');
header('Expires: 0');

// Redirect to login with success message
header('Location: login.php?logged_out=1');
exit;
?>