Jump to content

Creating a Users Online page


bachx

Recommended Posts

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!

Link to comment
Share on other sites

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>";
}

?>

Link to comment
Share on other sites

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");

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.