PHP /SESSION: Login one per user?

前端 未结 8 1682
臣服心动
臣服心动 2020-12-08 23:20

How can i do so only 1 can be online for the 1 user at the time? Idea ?

So you e.g can not login to User1 on one pc/browser and then on the other pc/browser login to

8条回答
  •  余生分开走
    2020-12-09 00:21

    I assume you save users in a database, add an active_session field, update it upon login, and check it on requests to ensure that current user session id matches the last one stored in the database.

    On Login:

    UPDATE `users` SET `active_session`='$session_id';
    

    When user goes to a page that requires login, you search that value:

    SELECT * FROM users WHERE `active_session`='$session_id';
    

    this way, if the user signs in other place, the previous session key gets overwriten, and the SELECT above returns an empty resultset.

提交回复
热议问题