Check if user is online
For readability, and simplicity, I like to use the core API:

Check if current user is authenticated

user_is_logged_in()

i.e:

if (user_is_logged_in()) {
  // User is authenticated
  print $content;
}


You can also use

if (!user_is_logged_in()) {
  // User is anonymous
  print $content;
}


though there's even a user_is_anonymous() function

if (user_is_anonymous()) {
  // User is anonymous
  print $content;
}


Check if particular user is logged in

If you want to check if a particular user is online

You can use Rules, or hook_user_login to respond to login event

EXAMPLE_user_login(&amp;$edit, $account){
  /* do stuff */
}


or query the session table for that user session uid

// Check if user $uid is online in the last 10 minutes
$online = db_query("SELECT uid FROM {sessions} WHERE uid = :uid AND timestamp &gt;= :time", array(':uid' =&gt; $uid, ':time' =&gt; (REQUEST_TIME - 600)))-&gt;fetchField();