开发者

PHP and SQL Social Networking - User Online

开发者 https://www.devze.com 2023-01-19 02:50 出处:网络
I want to set up online detection on my website. I have a row in 开发者_开发问答my user table where the last login datetime is stored. Every time a user visits the site, his login date updates and us

I want to set up online detection on my website.

I have a row in 开发者_开发问答my user table where the last login datetime is stored. Every time a user visits the site, his login date updates and user online row sets to 1 (1 - online, 0 - offline).

How to change the online row to 0 (offline) if the last login was 10 or more minutes ago? The aim is to find difference between dates.


cronjob every 10 minutes?

UDPATE users SET online = 0 WHERE login_date > (NOW() - INTERVAL 10 minute);


just to each user add a last_seen timestamp to there row so that when you do your user is authed check you can update the time

if(logged_in())
{
    update_user();
}


function update_user()
{
    //UPDATE users SET last_seen = unix_timestamp() WHERE uid = X;
}

Then you can do for you users:

SELECT * FROM users WHERE last_seen > (unix_timestamp()-300)

To get the last 5 mins.


If you want to show the users who have been online within last 10 mins then the best method is to include the datetime condition in the sql query.


Save the last login time as timestamp, then you can easily compare it with the current time and tell how much time has passed since.

Depending on the size of your user table, you can run the check of those who are still supposedly online every time somebody calls your website.

A different approach is to store active users in buckets, labeled with the last login time, you can then easily reset all users that are in buckets older than 10 minutes.

0

精彩评论

暂无评论...
验证码 换一张
取 消