简体   繁体   中英

how to update user status offline in php mysql

I am creating users status update script in PHP MYSQL. I have added strtotime update to status column within the 24 second. but my script not updating offline I want to add NOW() date time to update offline status column.

Here is my source code

<?php
$time=date("Y-m-d H:i:s",strtotime('-24 seconds', time()));

if($stmt = $con->prepare( "UPDATE users SET status = 'offline' WHERE time='".$time."' NOW()")){

$stmt->execute();
}
?> 

It is better to

// set offline flag on all inactive users
"UPDATE users SET status='offline' WHERE time < NOW() - INTERVAL 24 SECOND;"

// then flag current user as active
"UPDATE users SET status='online', time=NOW() WHERE id=321;"

Then you can get list of online users

"SELECT * FROM users WHERE status='online';"

or

"SELECT * FROM users WHERE time > NOW() - INTERVAL 24 SECOND;"

also it is better to store users status in a specified table
also use of memory database engine for status table
also use of integer instead of string; 0 ='offline' , 1 ='online'
also use of timestamp as integer instead of datetime

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM