PHP /SESSION: Login one per user?

前端 未结 8 1670
臣服心动
臣服心动 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:05

    This solution doesn't require you to access the database on every page and doesn't lock out the user after they failed to log out.

    Add a field for sessionID to your user table in the database.

    Set the default session handler before calling session_start():

    session_set_save_handler(new \SessionHandler());
    

    On every successful login, retrieve the stored $sessionID from the database. Destroy the old session with:

    (new \SessionHandler())->destroy($sessionID);
    

    Get the new session ID with:

    $sessionID = session_id();
    

    Store the new session ID to the database.

提交回复
热议问题