How to count the number of members currently viewing an action of your choice

Started by Biology Forums, September 05, 2014, 12:19:51 AM

Previous topic - Next topic

Biology Forums

I'm sharing this script upon request from this thread.

The script was developed with SMFHacks - respect goes to him for helping me put this idea together.

Note: This has only been tested on SMF 1.x, it will not work on 2.x, so feel free to alter it and share it below.

I placed this script inside load.php, specifically  inside the function loadUserSettings(), just before the }.

if (($context['gal_users'] = cache_get_data('gal_users', 15)) == null)
$context['view_members2'] = array();
$context['view_num_hidden'] = 0;

// Search for members who are in the gallery
   $request = db_query("
  SELECT lo.ID_MEMBER, lo.logTime, mem.showOnline
  FROM {$db_prefix}log_online AS lo
  LEFT JOIN {$db_prefix}members AS mem ON (mem.ID_MEMBER = lo.ID_MEMBER)
  WHERE lo.url like '%s:7:\"gallery\"%'", __FILE__, __LINE__);

while ($row = mysql_fetch_assoc($request))
  if (empty($row['ID_MEMBER']))

  $context['view_members2'][$row['logTime']] = array(
'id' => $row['ID_MEMBER'],
'hidden' => empty($row['showOnline']),
  if (empty($row['showOnline']))
// Sort the list.

// The number of guests is equal to the rows minus the ones we actually used.
$context['view_num_guests2'] = mysql_num_rows($request) - count($context['view_members2']);
$context['gal_users'] = $context['view_num_guests2'] + count($context['view_members2']);
cache_put_data('gal_users', $context['gal_users'], 15);

If you want another action besides action=gallery, change:


to something else, like stats:


Notice how s:7 became s:5 and gallery became stats.


Hj Ahmad Rasyid Hj Ismail

Biology Forums

Quote from: ahrasis on October 03, 2014, 09:44:13 AM
Thank you for sharing this Shuban. Can't wait to test it.

You're welcome. This will also work best if a caching system has been put in place.


Yeah because this is going to suck in performance terms :( There's no way the query can be optimised at all, so it ends up being a complete table scan of the log_online table, which is also full-blocking unless you're using InnoDB (which means if you have a large online list, it blocks everything else updating it while it fetches the data)

Hj Ahmad Rasyid Hj Ismail

Hah... That is a nice catch. Will bear it in mind while attempting this idea.