How to manage the online status of a user when he closes the browser?

I have a table for tracking the online status of a user by setting the "is_online" field to 1. Now the user logs on to the site and closes his system or shuts it down when he / she is logged in. Here in the database is_login there is a value of 1, which means that it is still using the site.

So I need to do some logic to change this value to "0".

How can I handle this situation using PHP?

+5
source share
5 answers

, , - . (, ) , .

. .

+10

"--". , - . is_online DateTime , -.

, , :

SELECT * FROM users WHERE is_online = 1

:

SELECT * FROM users WHERE is_online >= DATE_SUB(NOW(), INTERVAL 5 MINUTE)
+3

. HTTP , . , .

, JavaScript . php script, . , , , .

javaScript, : http://www.w3schools.com/JS/

, AJAX HTTP.

.. Roman

+2

jquery X , 10 , :

function updateTheBBDD(){
    ... calling mysql_connect -> mysql_insert -> mysql_close
}

setInterval("updateTheBBDD()",10000);

In addition, when drawing a user state monitor instead of consulting with BD and:

1->green
0->red

You should:

actual - time > 10  ---> red
actual - time <= 10 ---> green
+1
source

Due to the statelessness of the website, it is impossible to know exactly when the user left your website. Thus, you cannot initiate any code to change the specified value.

As others said, you need to save the operation “from time to time” in order to get an approximate idea when the last time the user really did something with the website.

Hth :)

0
source

All Articles