简体   繁体   中英

Generate a list of online users?

I'm not awesome enough to write a chat application, and I'm trying to get one to work, and I've recently downloaded one from here , it's pretty good so far, as I've tested it out on XAMPP, but I have a slight problem. I'm trying to generate a list of online users to give it a more practical application-like feel, but the problem with that, is I have no clue how to do it easily.

When users login to my site, a session named g_username is created, (the chat says 'username', but I'll fix that) and from what I see so far, the easiest method would be to store their username in a database called OnlineUsers and call that data via Ajax, but, the other problem, is that it's session based, and sometimes the users can just leave, without logging out, and I intended to run a script to logout the user from both the OnlineUsers table, and by deleting the session.

If they leave without logging out, they'd be online forever! I could potentially suffix a bit of code on every page, that toggled an ajax event on page close, the event being a script that kills their OnlineUsers table record, but then again, that would load the server with useless queries as users jump between pages, as far as I'm aware.

Creating my entire site in Ajax isn't really an option, as it's a load of different sites combined in to 1 'place' with a social 'layer' (if you will) from a social service.

Does anyone see a way to do this that would make sense, and be easy to integrate, and do with Apache, without command line access?

您可能每60秒左右在服务器上运行一次cron作业[如果有cpanel],该作业将检查用户最后一次通过聊天发送任何内容的时间(如果最后一次没有显示5分钟),然后从网上删除他们的条目用户列表。

You could so something like storing a timestamp of the users last action in a database, comparing that timestamp when outputting online users and making sure that it was done at most 1 min ago.

Run on all/vital pages: (Deciding if the last action is outdated, you could also check if it was done for one minute ago to reduce the database-load)

if($user['lastAction'] < time()) {
   //update into database, last action is outdated
}

When calculating the amount of users online and is within the loop of each timestamp

//If the users last action was within a minute, the user is most likely online    
if(($row['lastAction']- time()) > 60*60)
       //count user as online

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