<?php

    // online_checker.php
    // include('../index.php');

    // date_default_timezone_set("Africa/Lagos");

    // Function to check if a user is online based on their last activity
    function isUserOnline($username, $pdo) {
        // Define the timeframe (in seconds) to consider a user "online"
        $online_threshold = 120; // 5 minutes

        // Query the database to get the user's last activity timestamp
        $stmt = $pdo->prepare("SELECT recent_activity FROM user_activity WHERE username = :username");
        $stmt->bindParam(':username', $username);
        $stmt->execute();
        $row = $stmt->fetch(PDO::FETCH_ASSOC);

    

        if ($row) {
            // Calculate the time difference between current time and last activity
            $recent_activtiy_time = strtotime($row['recent_activity']);
            $current_time = time();
            $time_difference = $current_time - $recent_activtiy_time;

            // Return true if user's last activity is within the online threshold
            return $time_difference <= $online_threshold;
        } else {
            // User has no activity record (e.g., newly registered), consider them "offline"
            return false;
        }
    }

 ?>
