<?php
session_name('small_admin_session');
if (!is_dir(dirname(__DIR__) . '/session_data/small')) { mkdir(dirname(__DIR__) . '/session_data/small', 0700, true); } session_save_path(dirname(__DIR__) . '/session_data/small');
session_set_cookie_params(['path' => '/', 'httponly' => true, 'samesite' => 'Strict']);
session_start();
include("includes/dbconnection.php");

date_default_timezone_set('Africa/Lagos');
$ldate = date('d-m-Y h:i:s A', time());
$email = $_SESSION['email'] ?? '';

if (!empty($email)) {
    $sql = "UPDATE userlog SET logout=:ldate WHERE userEmail=:email ORDER BY id DESC LIMIT 1";
    $query = $dbh->prepare($sql);
    $query->bindParam(':ldate', $ldate, PDO::PARAM_STR);
    $query->bindParam(':email', $email, PDO::PARAM_STR);
    $query->execute();
}

$_SESSION['errmsg'] = "You have successfully logged out";

// Unset session variables
unset($_SESSION['sid'], $_SESSION['name'], $_SESSION['lastname'], $_SESSION['email'], $_SESSION['permission'], $_SESSION['login']);

// Destroy session
session_unset();
session_destroy();

if (isset($_COOKIE['small_user_login'])) {
    setcookie("small_user_login", "", time() - 3600, "/");
}

header("Location: student_search.php");
exit;
?>
