Take action when closing browser

1

I have a system that marks whether the person is available or unavailable . There is a button called exit . If the person clicks on it when it is available, then it automatically changes the status of the database to unavailable. Only some site users are clicking on the X , so the person is still available but in fact it is no longer logged in to the site. I've been researching and some people were talking about using the unload or onBeforeUnload function because the two have a difference. I would like to know which of these two to use and if by chance someone knows how to make a $ post request with jQuery when the person clicks on X     

asked by anonymous 25.09.2014 / 17:39

1 answer

1

You might consider using logic to determine whether the person is online or not! Example, create in your table in the database a field called ping_usuario so you can create an ajax function that calls that same url every 10 seconds.

  

Example:

setTimeout(function(){
    $.ajax({url:'ping_user.php'});
},10000);
  

ping_user.php file

<?php
     session_start();
     $login = $_SESSION['login']; // Aqui a variavel de sessão que identifica o usuario.

     //inclui seu arquivo de conexao.
     include('conexao.php');

     //Efetua um update.
     mysql_query("UPDATE tb_login SET ping_usuario=NOW() WHERE login='$login'",$link_conexao); // Aqui usei o mysql_query para efetuar o update porem eu recomendo que estude a iteração com o banco através do PDO.

?>

After that do the reverse, declare the person with an interval less than 20 seconds online.

SELECT * FROM tb_usuario WHERE ping_usuario <= DATE_ADD(NOW(), INTERVAL -20 SECOND)

So if the user closes or his energy ends up for example he will not update the ping, soon his search algorithm will identify and consider it offline.

* obs: the ping_user field must be of type DATETIME

    
25.09.2014 / 18:12