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
https://forums.phpfreaks.com/topic/41689-creating-a-users-online-page/
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>";
}

?>

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.

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.

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.