Is it possible to track how long a user has been logged in on a website using PHP?

Yes, it is possible to track how long a user has been logged in on a website using PHP by storing the login time in a session variable when the user logs in and then calculating the duration by comparing the current time with the stored login time.

// Start the session
session_start();

// Set login time in session variable when user logs in
if(isset($_SESSION['login_time'])) {
    $_SESSION['login_time'] = time();
}

// Calculate the duration of login
$login_duration = time() - $_SESSION['login_time'];

// Display the login duration
echo "User has been logged in for: " . $login_duration . " seconds";