Count logins for the same user

Is is possible to count, how many different places user logged in? I need to limit user can access page 3 pages at the time (not more) - is there any way for that? I thought maybe counting sessions would be an option, but not sure if session count would be accurate.

Currently I’m in the middle of limitation. But for some reason Kirby stuck after user is logged in (timeout). After refresh login works again. Do you see the problem, why it does that:

function usortSession($a, $b) {
    $store = new Kirby\Session\Sessions(kirby()->root('sessions'));
    $sa = new Kirby\Session\Session($store, $a['session_key'], []);
    $sb = new Kirby\Session\Session($store, $b['session_key'], []);
    
    $acta = $sa->get('lastActivity');
    if (empty($acta)) $acta = 0;
    $actb = $sb->get('lastActivity');
    if (empty($actb)) $actb = 0;
    
    return $acta - $actb;
}
  'hooks' => [
    'user.login:after' => function ($user, $session) {
      $m = $user->membership()->value();
      if (!empty($m))
        $login_limit = 1;

      //temp column to save session tokens      
      $column_login = $user->column_login()->value();
      
      kirby()->session()->ensureToken();
      $token = kirby()->session()->token();
      
      if($column_login == '' or count(json_decode($column_login, true)) == 0) {
        $logins = [
          ['session_key'=> $token, 'time' => time()]
        ];
      } else {
        $store = new Kirby\Session\Sessions(kirby()->root('sessions'));
        
        $logins = json_decode($column_login, true);

        //clear removed ones
        foreach($logins as $key => &$login){
          try {
            $s = new Kirby\Session\Session($store, $login['session_key'], []);
          } catch (Kirby\Exception\NotFoundException $e) {
            unset($logins[$key]);
          } catch (Kirby\Exception\LogicException $e) {
            unset($logins[$key]);
          }
        }

        //contains already
        $contains_already = FALSE;
        foreach($logins as &$login){
          if ($login['session_key'] == $token) {
            $contains_already = TRUE;
          }
        }

        if (!$contains_already) {
          if(count($logins) < $login_limit){
            $logins[] = ['session_key' => $token, 'time' => time()];
          } else {
            usort($logins, "usortSession");
            
            $sessionTo = array_shift($logins);
            try {
              $s = new Kirby\Session\Session($store, $sessionTo['session_key'], []);
              $s->destroy();
            } catch (Kirby\Exception\NotFoundException $e) {
            } catch (Kirby\Exception\LogicException $e) {
            }
            $store->collectGarbage();
            
            //after iteration we check if the count of logins is still greater than the limit
            if(count($logins) >= $login_limit){
              //then return a login error that maximum logins reached
              $user->logout();
              go('login');
            }else{
              //then login is successsfull
              $logins[] = ['session_key' => $token, 'time' => time()];
            } 
          }
        }
      }
      
      $user->update([
        'column_login' => json_encode($logins)
      ], 'en');
    }
  ],

what if the inital value of $column_login is not parseable as json? like if its an empty string?

Then this one works:
if($column_login == '' or count(json_decode($column_login, true)) == 0) {

Problem is when there is 1 user, wchich logins on another PC - it is successfully removed from sessions and list. The system executes if(count($logins) >= $login_limit) {…} else { $logins[] = [‘session_key’ => $token, ‘time’ => time()]; } path correctly, and then stuck.