Perform action when closing browser

Asked

Viewed 1,336 times

1

I have a system that marks if the person is available or unavailable . There is a button called get out of. If the person clicks on it when it is available, then it automatically changes the status of the database to unavailable. Only that some users of the site are clicking on X, thus the person continues as available only that actually she is no longer logged in to the site. I’ve been researching and some people talked to use the function unload or onBeforeUnload 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 request $.post() with jQuery when the person clicks on X

  • The purpose is to chat or see online users?

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 ai 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 as online the person with a range less than 20 seconds.

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

So if the user closes or his energy runs out for example he will not update the ping, so his search algorithm will identify and consider the same offline.

*Obs: the ping_user field must be of type DATETIME

  • Any questions about the implementation in your algorithm can contact me at http://www.hiago.me

Browser other questions tagged

You are not signed in. Login or sign up in order to post.