2

I was wondering if there was a method to show anonymous users in a view. I have looked at various alternative such as the link below for showing anonymous users. http://scito.ch/content/count-access-statistics-anonymous-users-only-drupal-7. I did construct a view for the number of registered users but am having a problems showing just anonymous users in a view

2

There is (and should only ever be) one anonymous "user" for your site. That will be in your db users table as uid 0.

If you'd like to count the number of anonymous users that are visiting your site at a certain time you can adapt (to a custom module) something like this that is used in the admin_menu module (this is from admin_menu.module 7.x.3.0-rc starting around line 395:

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' => $count_anon, '@count-auth' => $count_auth));
}

function admin_menu_session_count($timestamp = 0, $anonymous = TRUE) {
  $query = db_select('sessions');
  $query->addExpression('COUNT(sid)', 'count');
  $query->condition('timestamp', $timestamp, '>=');
  $query->condition('uid', 0, $anonymous ? '=' : '>');
  return $query->execute()->fetchField();
}

And then output the return values. If you want to count the all time number of anon visits to your site than that is a different story. If so, please update. Thanks.

  • So I can use that code in creating my own module. In That correct. How would I output the results in a view. – alex Aug 6 '12 at 12:01
  • Yes you can, you would have to change the function names to YOUR_MODULE_NAME_get_user_count and YOUR_MODULE_NAME_session_count and then in your view the quick way is to add drupal.org/project/views_php and call YOUR_MODULE_NAME_session_count in a php field. The better way as noted in the views_php module is to " to use regular handlers and plugins when available (or even to create one yourself)". – nedwardss Aug 6 '12 at 12:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.