Hey all,
I had originally been using a shell script and sessions based solution for this, but I just rewrote this to be much more simple using just PHP and MySQL. Perhaps someone will find it useful. 🙂
Plug in your own database info, set $minutes to how long you want users to stay as "Online" before they are cleared out of the list. Also, $setuserid is a session variable that is set when a user logs in, it contains their unique user ID number.
I include this code on another page that's on almost all my pages that contains the "Logged in as $username or Click Here To Login" type deal. 🙂
<?php
echo "<DIV ALIGN=LEFT><FONT SIZE='-4'><B>Users online</B>: </DIV></FONT> ";
$time = time();
$minutes = 10;
$minuteslong = $minutes * 60;
$timelimit = $time - $minuteslong;
if (isset($setuserid)) {
mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($database) or die( "Unable to select database");
$query1="UPDATE $users SET LASTONLINE = '$time' WHERE userid = '$setuserid'";
mysql_query($query1); }
$query="SELECT * FROM $users WHERE LASTONLINE >= '$timelimit'";
$result=mysql_query($query);
$num=mysql_numrows($result);
$i=0;
$userlist = '';
while ($i < $num) {
$username=mysql_result($result,$i,"USERNAME");
$userlist = "$username " . $userlist;
++$i; }
echo "<DIV ALIGN=LEFT><FONT SIZE='-4'>$userlist </DIV></FONT> ";
mysql_close();
?>