ANSWER
Below is the answer for anyone who has experienced something like this. First I created an extra field in my table called ActiveTime where I store the time () as soon as the user logs in. See below:
$tempoAtivo = time() + 30;
After storing, I created a new file called online that works as follows:
<script>
setInterval(function(){
$("#verOnline").load(location.href+" #verOnline");
}, 2000);
</script>
<div id="verOnline">
<?php
$tempoAtual = time();
$tempoAtivo = time() + 30;
$alterar = mysqli_query($conexao,"UPDATE proj_acessos SET Tempo = '".$tempoAtivo."' WHERE IdUsuarios = ".$_SESSION['IdUsuarios']."");
$alterar = mysqli_query($conexao,"UPDATE proj_acessos SET StatusOnline = 'N' WHERE Tempo < ".$tempoAtual."");
?>
</div>
The code above updates the database with the active time of the user every 2 minutes. If the user leaves the system directly through the browser, the time to update in the database and when the current time is greater than the time in the database, the status changes to offline.