Drupal 6: Custom Online Members Block

With the help of some PHP codes, showing a block that contains a list of your site's online members can be achieved (with a left floating picture beside each member name). The code was adopted from the Drupal 6 core, User Module. We all know that there is already a bundled block for this with Block name Who's-Online. But if you really want to do more about it, like adding a user picture, then you can do so with Blocks (I'm pretty sure you can do this with Views as well). And don't forget to enable PHP-Filter module. This can be found at admin/build/modules/list under Core-Optional modules. Then select PHP Code option among the list of Input Formats available when creating the new block.



<?php

if (user_access('access content')) {
    // Count users active within the defined period.
    $interval = time() - variable_get('user_block_seconds_online', 900);

    // Perform database queries to gather online user lists.  We use s.timestamp
    // rather than u.access because it is much faster.
  
    $authenticated_users = db_query('
        SELECT DISTINCT 
            u.uid, u.name, u.picture, s.timestamp 
        FROM 
            {users} u 
        INNER JOIN 
            {sessions} s ON u.uid = s.uid 
        WHERE 
            s.timestamp >= %d AND s.uid > 0 
        ORDER BY s.timestamp DESC'
        , $interval);

    $authenticated_count = 0;
    $max_users = 20;
    $items = array();
  
    while ($account = db_fetch_object($authenticated_users)) {
        if ($max_users > 0) {
            $items[] = $account;
            $max_users--;
        }
        $authenticated_count++;
    }

    // Format the output with proper grammar.

    $output = $authenticated_count == 1 ?
        t('There is only 1 member online.') :
        t('There are currently '.$authenticated_count.' members online.');


    // Display a list of currently online users.
    if ($authenticated_count) {
        foreach ($items as $u){
            $output .= '<div class="mini-user-panel">'."\n";
            $output .= theme('user_picture', $u);
            $output .= '<div class="name">'. theme('username', $u) .'</div>'."\n";
            $output .= '</div>';
        }
    }
  
    print $output;
}
      
?>



Bookmark and Share

1 comments :

Drupal Development Company said...

Nice drupal 6 custom information.

Post a Comment

Hi there! Please leave your message here. Also, I may not be able to respond to your query right away. So please bear with me. Thanks. :)