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

Asked

Viewed 280 times

0

I’m making a chat system for a panel, and I already have it ready practically, everything connected to the database, when it login changes to online and when it comes out(click the button) it changes to offline in db.

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

I don’t really understand the JS/Jquery and AJAX very well, so I’m a little lost in this question.

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

Code from offline.php

<?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 doesn’t end my session and only changes the status in the bank to offline, and when I open the page again, it changes to online, all this, without finishing the session or having to log in again.

1 answer

0

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

In these banks, there is usually the option of timeout for the database registration, then you can have ajax update the key and "reinvigorate" the cache timeout. Therefore, if the user ID is in the cache, it means it will be online, and otherwise offline.

I hope I’ve helped. ;)

  • Hello Paul, so at the moment I am with a certain deadline and not giving me much time to study new systems and implementation of the same in the codes. I don’t want to be ignorant or anything, I appreciate the help, but time is what screws me up.

  • You can adopt the same strategy for SQL then. Add a field like last_activity which will store a timestamp. This way it is possible to create some ajax connections to update the timestamp and can also be searched online users through a query that takes all records with the Tamp team greater than 10 minutes and display them as "online" for example.

  • I will try this way. Thank you beast ;D

  • Have you made any progress? @Thomasfranklin

Browser other questions tagged

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