Log out user when logging into another account [closed]

closed . This question needs to be more objective and is not currently accepting answers.

want to improve this question? update the question to focus on just one problem when edit it .

Closed 4 years ago .

improve this question

Hello I wanted to know how to disconnect a user if he enters another account in the same browser with the same IP, I use PHP session, if you can help me thank you very much!! For the user can log into as many accounts as he wants.

Author: Comunidade, 2016-07-28

1 answers

I believe that the best thing to do would be to store and query the database to check if there was no connection in another account.

For example:

When connecting this is created:

$_SESSION['conectado'] = true;
$_SESSION['id'] = '1';
$_SESSION['confirmado'] = time();

When you access another page (and make a request by ajax, anyway!):

if($_SESSION['confirmado'] < (time() - 300)){

  $query = mysqli_query('SELECT EValido FROM usuario WHERE id = "'.$_SESSION['id'].'"');
  $valido = mysqli_fetch_all($query);

  if($valido[0] === '1'){

     mysqli_query('UPDATE usuario SET EValido = 0 WHERE id != "'.$_SESSION['id'].'" AND ip = "'.$ip.'"');
     $_SESSION['confirmado'] = time();

  }else{

     // Não está conectado! 
     session_destroy();

  }


}

This is just one example!

This will cause in some situations the server to check if it is valid or not, doing the session_destroy if it returns that it is valid. Just check would be done after 5 minutes after last.

To update the value of EValido of the database you must check the IP (in this case) and compare with others already connected, so if another user with the same IP connect the old one will be disconnected.

Attention:

Public, open, and shared networks cause the same IP to be used for multiple devices and users. Therefore, disconnect users only because they have a even IP can be a big mistake and annoyance for many users, see if this is really necessary! In addition there are people who can own two internet providers, often using load balance , so the two IPs can be switched with each request, which can cause constant disconnection!

To make matters worse, there is a shortage of IPv4. Such a shortage means that several people, from the same provider, can own the same IP! It it occurs by the use of CGNAT. There is even there is a video about this created by NIC.br and There is also a post, with a supposed solution! I honestly don't know much about IPv6, but in December 2015 the usage of IPv6 was ~6.42%, I don't expect IPv6 usage to have expanded so fast. This only considering Brazil.

 3
Author: Inkeliz, 2020-06-11 14:45:34