Search code examples
phpsessionuser-accounts

How can I make sure that a user cannot login twice with the same account in PHP?


I have a PHP application with sessions, and want to make sure that the users don't give their credentials to third persons, which could use the account at the same time.

Is there any way to make sure that a user who logs in will kick out the first one ?

Is there also a way to view which sessions are active ?

Many thanks.


Solution

  • You can link the session id with the logged in user. Then look up if that user has a session id, if so destroy that session with that session id. Link the session name also. Then you can destroy the session cookie of that user.

    setcookie($session_name, '', time()-3600, '/');