Hi,

the user OneStone2000 has recently asked the same questions (http://drupal.org/node/46051) but didn't get an answer.

Does anyone know how I can display a list of (let's say) 5 members that have visited my profile page recently? Shouldn't be too hard, should it?

Let's try to find a solution, since I believe more Drupal users might be interested in such a feature.

Thanks guys,

Kai

Comments

newms’s picture

I would like to be able to add that feature as well. There is the Profile Visit module, but that just emails the user whenever someone views their profile (which I think may become annoying). It would be much more user friendly if Profile Visit were to record a list of members who visited your profile. Is there any way to do this?

newms

nedjo’s picture

which tracks page visits in the accesslog table. To implement this you would need a new module. In a hook_user function, use the 'view' op and something like this:


$result = db_query("SELECT uid FROM {accesslog} WHERE path = '%s' ORDER BY timestamp DESC LIMIT 5", 'user/'. $user->uid);
while ($row = db_fetch_object($result)) {
  $output .= l(theme('username', user_load($row)), 'user/'. $user->uid);
}

brakai295’s picture

Cool that sounds good.

I'm not able to test this until tmw. If anyone else wants to have a go at testing it?? Maybe there is a solution without needing to install the stats module?!

Cheers for your help so far!

Kai

Webdesign Melbourne Australia
www.brizk.com

frjo’s picture

I took the code from nedjos post above and put in profilevisitors.module that you can get from my sandbox.

http://cvs.drupal.org/viewcvs/drupal/contributions/sandbox/frjo/profilev...

I use it on my site vigillar.se and it seems to be working ok. I filter out anonymous visits as well as visits by a user to his/here own page.

brakai295’s picture

Hi there,

thanks for your answer. I'm fairly new to Drupal.

I have a customized user profile page (user_profile.tpl.php) - how would I implement it there?

Do I still need the statistics tool as mentioned above or does this script run independently?

Many Thanks

Kai

Webdesign Melbourne Australia
www.brizk.com

frjo’s picture

statistics.module is part of Drupal core, you simply need to activate it.

brakai295’s picture

hey, thanks! :-)

I have it enabled, but I would still need to know how I could implement it in the custimzed user profile page?!

Sorry to bother you...

Thx
Kai

Webdesign Melbourne Australia
www.brizk.com

brakai295’s picture

I inserted the following code on my user_profile.tpl.php but it doesn't seem to work:

function profilevisitors_user($type, &$edit, &$account, $category = NULL) {
  if ($type == 'view') {
    $nlimit = 5;
    $visitors = array();
    $result = db_query_range("SELECT al.uid 
      FROM {accesslog} al 
      WHERE al.path = '%s' AND al.uid != %s AND al.uid != 0 
      ORDER BY al.timestamp DESC", 'user/'. $account->uid, $account->uid, 0, $nlimit);
    while ($row = db_fetch_object($result)) {
      $visitors[] = theme('username', user_load($row));
    }
    if ($visitors) {
      $items[] = array(
        'title' => t('Recent visitors'),
        'value' => theme('item_list', $visitors),
        'class' => 'profilevisitors',
      );

      return array(t('History') => $items);
    }
  }
}

Can anyone tell me how I can make this work on my customized user profile page?

Webdesign Melbourne Australia
www.brizk.com

graggrag’s picture

The profilevisitors module is ok, but as I see it, we just want to show a simple list of registered users who have visited your profile in the last five days. All it really needs is a little function in user_profile.tpl.php. Here's the function I'm working on. It goes almost anywhere in user_profile.tpl.php -

<?php
function my_recent_visitors() {
  global $user;
  $ouput = '';
  $cnt = 0;
  $can_access_profiles = user_access( 'access user profiles' );
  $qry = db_query(
    "SELECT DISTINCT al.uid, u.name
    FROM {accesslog} al, {users} u
    WHERE al.path = '%s'
    AND al.uid > 1
    AND al.uid <> %d 
    ORDER BY al.timestamp DESC LIMIT 5", 'user/' . $user->uid, $user->uid );
  while ( $visitor = db_fetch_object( $qry )) {
    if ( $cnt > 0 )
      $output .= ', ';
    if ( $can_access_profiles ) {
      $output .= l( $visitor->name, 'user/' . $visitor->uid, array( 'title' => t( "View this visitor's profile." )));
    }
    else {
      $output .= check_plain( $visitor->name );
    }
    $cnt++;
  }
  return $output;
}
?>

Then in the appropriate place in the themed area,

<div id="my_recent_visitors"><?php print my_recent_visitors();?></div>

As that stands, it might just explode rudely. The system I'm working on at the moment only has two users set up, so that makes proper testing difficult right now, and right now I really need to get some sleep. I won't be able to get back to it until tomorrow night, so in the meantime, if anyone cares to try it, feel free!

Also, as well as having the statistics module enabled, I think you need to visit administer->settings->statistics and enable 'Enable access log' in 'Access log settings'.

Cheers.

graggrag’s picture

The accesslog table only has an index on timestamp, so I wonder how slow this might be on a busy site. Scouring the entire accesslog for user id and path might be tedious without some indexing on those fields.

brakai295’s picture

Hi Alan,

thanks vey much for all the effort you are putting into this!!!

I've got all the necessary statistics-settings enabled. This whole thing seems a lot more complicated than it is. And if it puts too much load on the server, we might have to forget about it at this stage. what do you think? Maybe anyone else has an idea?

Kai

Webdesign Melbourne Australia
www.brizk.com

chamara’s picture

Hello there,
This snippet works fine with 7.4.x. but the problem is it gives the same user list for each user profile page. Is there any way to avoid that and display the actual user list?

Chamara

Helge’s picture

The script seems to work, but doesn’t register all visitors of the page.
It only lists up the same users all the time, even though other people have been visiting the page.
In addition, the links to the users’ profiles which the system actually registers, always lead to one and the same profile (my own) no matter on which visitor profile you click.

Do you think it possible that drupal 4.7.4 causes the script to work incorrectly?

Does anyone know the problem?

regards
Helge

svogel’s picture

The code from graggrag is good as a start. I've used it and modified it.

There is a join missing in the sql between accesslog and users. My modified code looks like this:

function my_recent_visitors() {
  global $user;
  $can_access_profiles = user_access('access user profiles');
  $qry = db_query("SELECT DISTINCT al.uid, u.name FROM {accesslog} al, {users} u
    WHERE al.path = 'user/%d' AND al.uid > 1 AND al.uid <> %d AND al.uid = u.uid
    ORDER BY al.timestamp DESC LIMIT 5", $user->uid, $user->uid );
  while ($visitor = db_fetch_object($qry)) {
    $lOutput[] = ($can_access_profiles ? l($visitor->name, 'user/' . $visitor->uid, array('title' => t("View this visitor's profile.")))
      : check_plain($visitor->name));
  }
  if (count($lOutput))
    return '<ul><li>'.implode('</li><li>', $lOutput).'</li></ul>';
}

I've not tested this with lots of users, so this might not be the fastest method.

netranger’s picture

This project sounds great. I need something like this also.

nancydru’s picture

I like that use of Implode - I've never thought of that one.

You get whether the access privilege, but then you never check it. But Drupal is going to check it anyway, so you can probably remove that line.

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

fago’s picture

a great functionality. I think it would be great to have user statistics/accesslog views integration in conjunction with usernode. Then one could just create a view for that.

svogel’s picture

... yup, implode is nice :-)

I'm using the access privilege, but I must admit, it is hard to find.
If someone has the access privilege the user-name is linked to the user-profile. Else only the user-name is listed.

Best regards
Stefan

Prasad Shir’s picture

Great code!

This is working for Drupal 5.1 also...

nancydru’s picture

1) Make sure you add global $user; to know who the current user is.

2) Make sure you keep your access logs long enough to make this code work for you. For example, if you only keep them for a day you might think no one has looked at your profile when, in fact, it was viewed 4000 times yesterday. If you want to be more accurate, you might want to add a CRON extract routine that runs daily.

3) This does not need to be in a module. It would work just fine as a php page where it would be less prone to breaking your site..

Nancy W.
Drupal Cookbook (for New Drupallers)
Adding Hidden Design or How To notes in your database

Hara Kim’s picture

This code seems great nice work guys I will defiantly use this in my new sites!

www.hutsy.com - Search Engine, Web Directory & Online Community
www.asiankraze.com - the online asian community
www.moneymakerfuel.com - money making portal, forums
www.dexurl.com - www.justyourpet.com - www.realcasinotips.com - www.seopagerank10.com

Lausch’s picture

i try to find a solution to list the referer also.

how i can do this? more informations on http://drupal.org/node/171356

greets

Lausch

GolDRoger’s picture

It's possible change the script to record node profiles? For example: profile/user1

nancydru’s picture

That info would be recorded in the accesslog.

GolDRoger’s picture

You mean I have only to change this part of the script , changing user/%d in profile/%d ?

WHERE al.path = 'user/%d' AND al.uid > 1 AND al.uid <> %d AND al.uid = u.uid

Sorry I'm a noob in php, If u could make me an example it's very appreciated :)

function my_recent_visitors() {
global $user;
$can_access_profiles = user_access('access user profiles');
$qry = db_query("SELECT DISTINCT al.uid, u.name FROM {accesslog} al, {users} u
WHERE al.path = 'user/%d' AND al.uid > 1 AND al.uid <> %d AND al.uid = u.uid
ORDER BY al.timestamp DESC LIMIT 5", $user->uid, $user->uid );
while ($visitor = db_fetch_object($qry)) {
$lOutput[] = ($can_access_profiles ? l($visitor->name, 'user/' . $visitor->uid, array('title' => t("View this visitor's profile.")))
: check_plain($visitor->name));
}
if (count($lOutput))
return '

  • '.implode('
  • ', $lOutput).'

';
}