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.
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.
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
if you still need help please email me, axilant(AT)gmail.com
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>