Modify the user table with a command like this:
alter table users add last_login int;
Include the following in the login script as well as in the header that gets included on every page:
$now = time();
$query = "update users set last_login=$now where id=$userid";
Now, when you display a user's list of friends, do a query on each friend to see what their last login was and execute the following command:
if (time() - $last_login < 5 * 60) { print "Logged in"; } else { print "Logged out"; }
This will show that people are logged in if they have hit any page on your web site in the last 5 minutes.
As a nice side effect, you now also have a way to make people's sessions automatically time out. Just add this to the header script before you update their record:
$query = "select last_login from users where id=$userid";
...
if (time()-$last_login > 5 * 60) {
print "Your session has timed out. You must login again.<p>";
include("login.php"; exit; }
For security, make sure that you store their userid in the session variable and not a cookie.