How to detect if a user has logged out, in php?

后端 未结 12 2473
南笙
南笙 2020-12-06 02:19

After the user successfully logs in, I store login = true in database. But how do I check if the user logged out by closing the browser without clicking the logout button? A

12条回答
  •  伪装坚强ぢ
    2020-12-06 03:10

    IMHO the best way is to store last activity timestamp in DB on each update of user record. After logoff or timeout (maintain timeouts with cronjob) just set it to zero-value and use as flag.

    $user = new User($user_id);
    $user->logged_in = (bool)($last_activity > 0);
    

    Sometimes you will need to say smth. like "last seen on ...", then leave last activity and just add a boolean flag (tinyint) logged_in to your users table.

提交回复
热议问题