Actually, I had to do something similar on my page. I made an online system to tell other member when their friends were online. What happend was, when a user logged in (any user) I set their Online field (in the database) = 1. Ths meant that they were online. The only time that field got set = o was when they logged out. Also when they logged in, a timestamp was taken so I could say when the last logged in etc. This timestamp was help in a filed called LogTime.
This is where I realised that I has a problem because most of the users were logging in, and not logging out. So the system was saying that they were online when they really weren't.
This is how I over came the problem.....
header.php if a file that get's included in every document. It contains the basic structre of the page. I added this at the very top of it.......
$Time = time();
mysql_query("UPDATE Member SET Online = 0 WHERE LogTime < $Time;");
That's what I did. You can so something like this......
$Time = time();
mysql_query("DELETE FROM (table) WHERE (timestampfield) < $Time;");
Hope that will help you.
~~Stereo