It's not so difficult to show a list of the current visitors. There are modules for that, and the code isn't too difficult to bake yourself.
But is it also possible to have a list of the current visitors PLUS the pages (oh well, the nodes) they are visiting??? If so, of course: how???

Comments

mtsanford’s picture

I wrote a module that does something similar that involves tracking user's node visits.

Basically you need a module to stash away the last visited page for each user in a table. The crux of it is this:

/**
 *  hook_init()
 *  Keep track of the last node visited by each user
 */
 function user_sync_init() {
  global $user;

  /** 
     * IGNORE FRONT PAGE LOADS
     * Some modules may be buggy and cause the browser to fetch the root document rather than a file
     *  Drual will reroute that to the <front> page, causing an eroneous page load.
     *  we do not want to register that as a page visit...
     */
  if (empty($_SERVER['QUERY_STRING'])) {
    return;
  }
  
  /** 
     * IGNORE PREFETCH!
     * Prefetch is a cool firefox feature, but messes things up when there is a <link rel="next"> tag created by
     *  modules like 'book' 
     * !TODO other browsers support prefetch?
     */
  if (strcmp ($_SERVER['HTTP_X_MOZ'], 'prefetch') == 0)
    return;
  
  $matches = array();
  if (preg_match('/^node\/([0-9]+)$/', $GLOBALS['_GET']['q'], $matches)) {
    if (user_is_logged_in()) {
        $result = db_query("SELECT * FROM {user_sync_nodes} WHERE uid = %d", $user->uid);
        if (($follow_rec = db_fetch_object($result)) == FALSE) {
          // The user does not have a record yet.  Create one.
          $follow_rec = new stdClass();
          $follow_rec->uid = $user->uid;
          drupal_write_record('user_sync_nodes', $follow_rec);
        }
        $follow_ok = $follow_rec->allow_follow;
        
        if ($follow_ok) {
          $follow_rec->nid = (int)$matches[1];
          drupal_write_record('user_sync_nodes', $follow_rec, 'uid');
        }

      }
  }
}

nevets’s picture

If you only care about node visits a simpler approach would be to use hook_nodeapi and the case where $op = 'view' since it gets rid of the path parsing.

mtsanford’s picture

The problem with this is that other nodes can be loaded on the same page load (through views, etc.), and you don't know if it's for the main content or not.

nevets’s picture

The last parameter tell you if it's a page load. And if you don't trust it you can always compare $node->nid to arg(1).

mtsanford’s picture

Ah, indeed.

You'll still need to check for page prefetching by the browser (only firefox as far as I know).