Change the status (online / offline) in the bank when closing the page

0

I'm doing a chat system for a panel, and I already have it ready practically, everything connected to the database, when it logs on it switches to online and when it exits (clicks on the button) it changes to offline in db. / p>

The problem is, when the user closes the page, I need the database to be changed to offline, I tried to use onbeforeunload as I saw it in a forum to update the code as below, I tried to use it make the change when it opens the page again, but it is not working, I think by a code hierarchy.

I do not quite understand JS / JQuery and AJAX very well, so I'm a little lost on that.

window.onbeforeunload = function () {
        atualiza_fecho();
    };
    function atualiza_fecho(){
        $.get('assets/inc/offline.php', function(resultado){})
    }

Offline.php code

<?php
session_start();
$session_id = $_SESSION['uid'];
$mysqli = mysqli_connect('localhost', 'root', '');
mysqli_select_db($mysqli, 'cbr_chat');
$q=mysqli_query($mysqli, "UPDATE users SET status = 'offline' WHERE uid = '$session_id'");
?>

The idea is, I closed the page, it does not end my session and only changes the status to offline in the database, and when I open the page again, it changes to online, all without ending the session or having to log in again.

    
asked by anonymous 07.07.2017 / 16:21

1 answer

0

Probably the most viable alternative is to use some key / value bank for this, such as Redis or mcache .

In these banks, there is usually the timeout option for the bank record, so you can have ajax update the key and "refresh" the cache timeout. Therefore, if the user ID is in the cache, it means that it will be online, otherwise offline.

I hope I have helped. ;)

    
07.07.2017 / 16:27