<?php
/**
 * Logout Page - SufarmaERP
 * Clears sessions and cookies and redirects to login.
 */
if (session_status() === PHP_SESSION_NONE) {
    session_start();
}

$_SESSION = array();

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

session_destroy();

// Redirigir a login.php
$scriptName = $_SERVER['SCRIPT_NAME'];
$sufarmaErpPos = strpos($scriptName, '/SufarmaERP/');
$basePath = ($sufarmaErpPos !== false) ? substr($scriptName, 0, $sufarmaErpPos + 12) : '/SufarmaERP/';

header('Location: ' . $basePath . 'login.php');
exit;
