<?php

session_start();
// Check if the user is logged in
if (!isset($_SESSION['alogin'])) {
    header("Location: adminlogin.php");
    exit();
}

// Include database connection
include('includes/db.php');

// Fetch user data from the database based on the username in the session
$username = $_SESSION['alogin'];
$query = "SELECT * FROM admin WHERE UserName = ?";
$stmt = $conn->prepare($query);
$stmt->bind_param('s', $username);
$stmt->execute();
$result = $stmt->get_result();
$userData = $result->fetch_assoc();
$stmt->close();
?>

<?php 

// Validate user session
if (!isset($_SESSION['alogin']) || !isset($_SESSION['stamp'])) {
    // Redirect to the login page if the user is not logged in
    header("Location: adminlogin.php");
    exit();
}

// Validate session timeout (adjust the time as needed)
$sessionDuration = 900; // Session duration in seconds (5 minutes)
if ($_SESSION['stamp'] + $sessionDuration < time()) {
    // Session expired, log out the user
    session_unset();
    session_destroy();
    header("Location: adminlogin.php");
    exit();
}

// Update session stamp on each page change
$_SESSION['stamp'] = time();
?>
