更改数据库中的客户端数据而不刷新页面

I have a user system and I use cookies to make them automatically logout after 1 hour of inactivity, but only if the client refresh the page it would update the database row. This is my code so far:

if(isset($_SESSION['username'])) {
    $activeUser = $_SESSION['username'];
    if(isset($_COOKIE['active'])) {
        setcookie('active', $activeUser, time() +3600);
    } else {
        $makeOffline = $DB->query("UPDATE users SET user_loggedin = '0' WHERE user_id = '".$dbid."'");
        header("location: ".$site_url."/logout.php");
    }
}

What I'm asking now is if there's any way I could set user_loggedin to 0 after 1 hour of inactivity without that the client have to refresh the page?

A better way is :

  1. store the active time into database.

  2. update that active time once this user refreshes or does something.

so you can get if this user is still online or not based on the record + 1 hour.