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

后端 未结 12 2490
南笙
南笙 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 02:56

    2017 edit: These days, your best bet is using websockets to track presence on a page/site.


    You cannot detect when a user closes their browser or navigates off your site with PHP, and the JavaScript techniques of doing so are so far from guaranteed as to be useless.

    Instead, your best bet is most likely to store each user's last activity time.

    • Create a column in your user table along the lines of 'last_activity'.
    • Whenever a user loads a page, update their last_activity to the current time.
    • To get a list of who's online, just query the database for users with last_activity values more recent than 10/20/whatever minutes ago.

提交回复
热议问题