Disconnect user when entering another account [closed]

2

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

    
asked by anonymous 28.07.2016 / 05:45

1 answer

3

I think the best thing to do would be to store and query the database to see if there was no connection to another account.

For example:

When you connect, this is created:

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

When you access another page (and make a request for 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 an example!

This will cause the server to check whether it is valid or not by doing session_destroy if it returns that is valid. Such verification would be done after 5 minutes after the last.

In order to update the value of EValido of the database you should check the IP (in this case) and compare with other already connected, so if another user with the same IP connect the old one will be disconnected. >

  

ATTENTION:

     

Public, open, and shared networks make the same IP used for multiple devices and users. So disconnecting users just because they have the same IP can be a big mistake and uncomfortable for multiple users, see if that's really necessary! In addition there are people who can own two internet providers, often using load balance , so the two IPs can stay alternating with each request, which can cause constant disconnection!

     

To make matters worse, there is a shortage of IPv4. This scarcity means that several people, from the same provider, can have the same IP! This is due to the use of CGNAT. there is even a video about it created by NIC.br and there is also a post , with a supposed solution! I honestly do not know much about IPv6, but in December 2015 the use of #, I do not expect the use of IPv6 has expanded so fast. That's just considering Brazil.

    
28.07.2016 / 08:33