How to properly deploy the user after a downtime?

Asked

Viewed 6,293 times

3

REPLY

Below is the answer for those who have been going through something similar. I first created an extra field in my table called Tempoative where I store the team() as soon as the user logs in. See below:

$tempoAtivo = time() + 30;

After stored, 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 above code updates the database every 2 minutes with active user time. If the user leaves the system directly through the browser, the time stops updating in the database and when the current time is longer than the time in the database, the status changes to offline.

  • The question has been edited and is now completely different from the original, and the answers now seem to have nothing to do with the question. if the question has changed, another is created. if it has not changed, more information is added to the original. so it does not seem right to me.

  • Joseph, please avoid editing your question so drastically, otherwise the existing answers are invalidated. Also don’t add an answer to the question itself. Finding the solution on your own is excellent, congratulations! But then you can answer your next question using the form below this page. I reversed your edit and added the original question with the answer. I suggest you put the second part as a new answer, then edit the question and remove that section from there. You can also mark your own answer as right if you feel you should do it. Hug!

  • the logic is simple and the implementation can take many forms. One way is by using Node.js. Several examples are found on the web.. https://simplapi.wordpress.com/2012/07/02/node-js-websocket-near-real-offlineonline-check/

2 answers

3


You’ll have to do something like this:

if (empty($_SESSION)) session_start();

if (empty($_SESSION['lastAccess'])) $_SESSION['lastAccess'] = time();
else {
    $_SESSION['lastAccess'] -= time();
}

if ($_SESSION['lastAccess'] > time()+5) {
    session_destroy();
    echo('session timeout');
}

Explaining:
If there is no Session, start a;
If there is no "last access" then the last access is equal to now;
Or else, remove the now to the time of the last access
if the last access is greater than now more 5 seconds, then the session has expired

This is a bit pseudo-code that I no longer have a PHP environment to test the code

As for the second doubt, someone in the English SO did a wonderful answer

  • Okay, Mosh, but in that case we won’t bump into what Blunt said about updating the page, since it runs on the server?

  • Since we recorded the lastAccess in session user And we’re beating the lastAccess against the time the page is being Loaded; No, don’t bump into what @Blunt said :)

  • Perfect Mosh. I’m going to do a test here with your code....

  • 1

    @Jose.Marcos later says if it worked and if it changed anything, to see if I need to change something in the answer :)

  • @Mosmage, a thousand pardons. I’m still getting used to the site. Your code worked correctly and I put as a response. Thank you. Hug.

2

php code is only executed once, when the page is loaded.

This doesn’t do anything:

if($segundos > $limite){
    session_destroy();
    echo "<script>alert('Sua sessão acabou!'); window.location.href='../index.php';</script>";
}else{
    $_SESSION['Time'] = time();
}

unless the variable $seconds is greater than the $limit when the script is executed, or if the page is constantly being refreshed (something that should not be done).

This validation should be on the client side, in javascript.

  • Sorry for the delay in answering... right Blunt, you would have some example to show me, because I looked for something on the Internet and did not find... I believe I did not look in the right way...

  • There is session_cache_expire(). From what I understand, it is renewed every time the user accesses session_start(), correct? Now if the user is in the chat for example of a given page and the time is for 15 min, you run the risk of session_cache_expire(15) to drop the user?

  • @Mark example of what more specifically? What is it that you are not able to do?

  • Hello Blunt.. Moshmage gave me a PHP code for inactivity, but I believe it has to be client-side in javascript. I need that if the user is long inactive, he is disconnected from the system and redirected to the login page. This would be an output for what I am looking for in relation to the user closing the browser without exiting through the system. When it does so, user status goes online without being...

  • 1

    @Jose.Marcos one of the ways to do this is to have a javascript code that from X in X seconds will fetch a page ex. ping.php. This page should return a different result if the session has already expired, in which case javascript redirects the user to the login page

  • Okay Blunt... forgive me the ignorance, because I am layman in javascript, you would have some example?

  • I got this code, but it directs even by clicking or moving the mouse: var timeout; Function set(){ timeout = setTimeout(Function(){ window.location.href = window.location.href = "index.php"; }, 120000); } Function clear(){ clearTimeout(timeout); } window.onload = Function(){ Document.onmousemove = Function(){ clear(); set(); } Document.onclick = Function(){ clear(); set(); } ;}

  • @Mark.I have here no example I can give you. the simplest will be to use jQuery and an ajax request, but without knowledge will not be easy. my suggestion is to go to the jquery website and look for examples of $.ajax

Show 3 more comments

Browser other questions tagged

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