Hello, I have what would otherwise be a simple request. I'd like to count the total number of authenticated users on the site, and I use this module for my sessions. I see the function sess_count() in the code with the note "not implemented." My current understanding is that the memcache session support is non-persistent (per #934706: DB writethrough for session information request to write-through to the DB.) So counting sessions in the database isn't going to be terribly helpful.

What do you recommend for this? It sounds like obtaining and counting all memcache objects is not ideal. Would that other issue be the ideal approach? Or would you recommend a different way to maintain a counter? I'd dovetail to the writethrough issue but I don't care as much about restarting the session, I just want to count.

Support from Acquia helps fund testing for Drupal Acquia logo

Comments

yonailo’s picture

Hello there,

I have applied the following hack to have sess_count functionality.

in the sess_write() function, I have added the following:

    //XXX: Hack to count anonymous users and authenticated users, we use the sessions bin to save these data
    if(!$user->uid) {
      $anonymous_count = dmemcache_get('anonymous_count', 'session');
      $anonymous_count[$user->sid] = $session->timestamp;
      dmemcache_set('anonymous_count', $anonymous_count, 0, 'session');
    }
    else {
      $authenticated_count = dmemcache_get('authenticated_count', 'session');
      $authenticated_count[$user->sid] = $session->timestamp;
      dmemcache_set('authenticated_count', $authenticated_count, 0, 'session');
    }

Then I have coded sess_count like this:

function sess_count($timestamp = 0, $anonymous = true) {
  $cnt = 0;

  if($anonymous) {
    $anonymous_count = dmemcache_get('anonymous_count', 'session');
    foreach($anonymous_count as $sid => $time) {
      if($time > $timestamp) {
        $cnt++;
      }
    }
  }
  else {
    $authenticated_count = dmemcache_get('authenticated_count', 'session');
    foreach($authenticated_count as $sid => $time) {
      if($time > $timestamp) {
        $cnt++;
      }
    }
  }

  return $cnt;
}

Hope this helps.

Jochus’s picture

FileSize
3.79 KB

Great idea, but the problem is: your $anonymous_count and $authenticated_count arrays will grow, and grow, and grow. Right?
I think you need to unset the $sid, so you can just hit count() on the array?

Please look at my patch

Jochus’s picture

FileSize
4.11 KB

Hmm, my previous patch isn't correct. My sess_count() doesn't look to the $timestamp argument. This is the better one

rjbrown99’s picture

Status: Active » Needs review

Patch here, changing status.