How to count the number of anonymous users that are currently using the site?
The admin_menu module does this, and provides this function:

/**
 * Counts how many users are active on the site.
 *
 * Counts how many users have sessions which have been active since the
 * specified time. Can count either anonymous sessions or authenticated
 * sessions.
 *
 * @param $timestamp
 *   A Unix timestamp. Users who have been active since this time will be
 *   counted. The default is 0, which counts all existing sessions.
 * @param $anonymous
 *   TRUE counts only anonymous users. FALSE counts only authenticated users.
 *
 * @return
 *   The number of users with sessions.
 *
 * @todo There are mostly no anonymous sessions anymore. Split this into a
 *   separate module providing proper user statistics.
 */
function admin_menu_session_count($timestamp = 0, $anonymous = TRUE) {
  $query = db_select('sessions');
  $query-&gt;addExpression('COUNT(sid)', 'count');
  $query-&gt;condition('timestamp', $timestamp, '&gt;=');
  $query-&gt;condition('uid', 0, $anonymous ? '=' : '&gt;');
  return $query-&gt;execute()-&gt;fetchField();
}


It returns the count of users who have been active in the past (default) 15 minutes like this:

    function admin_menu_get_user_count() {
      $interval   = REQUEST_TIME - variable_get('user_block_seconds_online', 900);
      $count_anon = admin_menu_session_count($interval, TRUE);
      $count_auth = admin_menu_session_count($interval, FALSE);

      return t('@count-anon / @count-auth', array('@count-anon' =&gt; $count_anon, '@count-auth' =&gt; $count_auth));
}