Track users currently logged in

i am in the process of building a forum from scratch and would like to know how to track the users that are currently logged on. ie like at the bottom of most vbulletin websites it says something like “there are currently 310 users viewing the boards(100 members 210 guests)”.

if someone could please point me to a tut or explanation of how this works.

thanks

I’m afraid with php the only way is to have a number (the count) and then when the user entersa page, php checks if that IP was logged in 10 minutes (or whatever the interval is) ago, and if it wasn’t then it adds the IP and the current time to the database/file, if it was then it does nothing. And to display it it just counts the active users in the last 10 minutes.

But I heard that with asp it is possible to determine when a session is destryed and the rest is simple… when a session is destroyd: decrease count, on creation: increase count.

i understand what you are saying…this should be possible with php and work well enough as vbulletin uses php and pulls it off.

any more advise?

thanks

nah… google for ‘users online tutorial’ or something for tutes

ok im going to explain how you can do this, for in real time with php

Have a field in your users table called online

and have it updated with:


 $date = date("U");
 $update = mysql_query("UPDATE table SET online = '$date' WHERE username = '$_COOKIE[user_cookie]'");
 
 //Have that in all the member pages
 
 //Now for the output of who is online:
 $time = date("U");
 $time2 = $time - 700;
 $sql=mysql_query("SELECT * FROM table WHERE online >= '$time2'") or die ("cant do it");
 while($row = mysql_fetch_array($sql))
 {
 print("$row[username]<BR>");
 }
 

Thats how you would do that part, just get the num_rows for the online users.

and for guests, just make a new table in with the users ip, and then do the same with the online part. and so on, i think if you have php knowledge you know what i mean :slight_smile:
if you still need help please email me, axilant(AT)gmail.com :slight_smile:

Why not setting logged in users to status 1 or active ?? than do a count(*) WHERE

That together with sessions is my guess all u need. The timer is c00l tho but not sure if that should be ure only check point

how accurate is it to use the IP address? won’t it change over time for most machines?

Well not that much… think about it they could use proxy… will they change, yes some do some dont ( most do these days )

The problem i see is not the count(*) online users its to check wether they actually closed there browser, for this javascript might actually come in handy
never done it like this however…

it can be easely used with php. consider the following


<script language="javascript">
<!-- hide from old browsers
function browser_is_closed() {
// if window.close = true !
<?php
// query 
?>
}
//-->
</script>