bachx Posted March 7, 2007 Share Posted March 7, 2007 I'm currently creating a Users Online page for my php forums. And I was wondering about a couple of things. When the user logs in, the script I created saves the username/session_id into a DB table called users_online, and lists the elements of that table on the Users Online page. All good and nice except for two things: - How can I remove a user from the online list (DB?) when his session times out and he's offline? I wrote a simple function that remove him from the Users Online page if he clicks the logout link, but what if he simply closes the browser window? He'll just stay on the online list. - How can I prevent duplicate usernames from showing up in the users online page? For example, I log into my account from two different browsers, each with a different session, and it shows the username twice! What to do? I'm not home right now so I could not paste any code, but any general tips/help would be greatly appreciated. Thanks! Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 7, 2007 Share Posted March 7, 2007 In your users table where it holds their username and whatever else, make a column called "last_logged_in" and everytime they click on a page make it update with the current time/date. Then on the online users list all you have to do is use a query that will pull all the users out of the database that have logged in within the last X minutes. Here is an example of a query that will grab all the users out of the database that have been active within the last 2 minutes: <?php $query=mysql_query("SELECT username FROM `users` WHERE `last_logged_in` > (NOW() - INTERVAL 2 MINUTE) ORDER BY id ASC")or die(mysql_error()); while ($row = mysql_fetch_assoc($query)){ echo "{$row['username']}<br>"; } ?> Quote Link to comment Share on other sites More sharing options...
bachx Posted March 7, 2007 Author Share Posted March 7, 2007 Thanks, will try that. What about the other issue? Is there any specifc PHP code that doesn't display duplicate/similar elements in an array? For example, I want the element 'x' printed only once in the below array: $array1 = array("x", "y", "z", "x", "x"); Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 7, 2007 Share Posted March 7, 2007 Well, with my script above it won't show 2 of the same users online no matter what...not sure if that is what you are looking for or not. I'm not sure how you would only display the "x" in the array once... Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted March 7, 2007 Share Posted March 7, 2007 In your users table where it holds their username and whatever else, make a column called "last_logged_in" and everytime they click on a page make it update with the current time/date. Then on the online users list all you have to do is use a query that will pull all the users out of the database that have logged in within the last X minutes. Here is an example of a query that will grab all the users out of the database that have been active within the last 2 minutes: <?php $query=mysql_query("SELECT username FROM `users` WHERE `last_logged_in` > (NOW() - INTERVAL 2 MINUTE) ORDER BY id ASC")or die(mysql_error()); while ($row = mysql_fetch_assoc($query)){ echo "{$row['username']}<br>"; } ?> How about people who have been browsing the forum for over two minutes? You'd rather make a sessions table where you update their last activity timestamp and check against that instead. Quote Link to comment Share on other sites More sharing options...
bachx Posted March 7, 2007 Author Share Posted March 7, 2007 I agree, what about people simply idling in the forums for more that 2 minutes? They will be counted as offline aswell. Oh and Daniel0, can you elaborate a bit about your suggestion please? A simple code maybe, as that might seem to work. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 7, 2007 Share Posted March 7, 2007 Daniel0 - Everytime they click it will update the "last_logged_in" field. So whenever they clicked in the forum it would update it and keep them on the online list. If you are talking about them staying on one forum post for two minutes, it will still update right after they are done and click again. I don't really understand how your idea is much different...or if it will even produce different results. That session update in the other table still isn't going to be updated until they click, so it will be the same thing. Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted March 7, 2007 Share Posted March 7, 2007 Well, I just assumed that the last_logged_in field would hold the timestamp of when they actually did log in, but if it actually holds their last activity timestamp, then the result will be the same. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 7, 2007 Share Posted March 7, 2007 Yeah, I guess I should have called it "last_activity" instead, hah. It would be a lot easier than having that extra table and having to find a way to delete unactive people. Quote Link to comment Share on other sites More sharing options...
bachx Posted March 8, 2007 Author Share Posted March 8, 2007 Ok guys, seems no one noticed my reply. What about people idling for more that 2 minutes on the forums (i.e Browsing the forums but not actually clicking anything?). Will those be accounted as offline or what? Thanks for the responses so far btw Quote Link to comment Share on other sites More sharing options...
Daniel0 Posted March 8, 2007 Share Posted March 8, 2007 @bachx: It depends on how you do it. If you update the a field in the database telling when the user was last active it will, if not, then it won't. Quote Link to comment Share on other sites More sharing options...
pocobueno1388 Posted March 8, 2007 Share Posted March 8, 2007 bachx - If they are reading a forum post for more than 2 minutes they won't show up on the online list until they click again. You may adjust the length to how long it takes before they don't show up. Change it from 2 minutes to 4 minutes if you want. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.