Show "members that have recently visited your profile" on user profile pages

brakai295 - September 25, 2006 - 02:53

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

I would like to be able to

newms - September 25, 2006 - 02:59

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

Any solution would probably need to depend on statistics.module

nedjo - September 25, 2006 - 03:36

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:

<?php
$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);
}
?>

Cool that sounds good. I'm

brakai295 - September 25, 2006 - 03:54

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

profilevisitors.module in my sandbox

frjo - September 25, 2006 - 07:02

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.

Hi there,

brakai295 - September 25, 2006 - 11:58

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

statistics.module is part of

frjo - September 26, 2006 - 04:39

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

how to implement it in the custimzed user profile

brakai295 - September 26, 2006 - 08:04

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

I inserted the following

brakai295 - September 26, 2006 - 10:20

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

Here's my approach ...

graggrag - September 28, 2006 - 11:57

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.

Another point

graggrag - September 28, 2006 - 12:05

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.

Hi Alan, thanks vey much for

brakai295 - September 29, 2006 - 04:21

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

Problem

chamara - January 1, 2007 - 08:20

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

some code problems with 4.7.4

Helge - March 2, 2007 - 15:34

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

Missing join

svogel - April 16, 2007 - 19:21

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.

This project sounds great. I

netranger - April 23, 2007 - 07:03

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

Interesting

NancyDru - April 23, 2007 - 14:27

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

a great functionality. I

fago - April 28, 2007 - 16:47

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.

Thanks

svogel - September 6, 2007 - 13:20

... 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

Working for 5.1 also!

Prasad Shir - May 22, 2008 - 11:35

Great code!

This is working for Drupal 5.1 also...

Several clarifications

NancyDru - April 23, 2007 - 14:24

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

This code seems great nice

hitboy - April 28, 2007 - 16:20

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

i try to find a solution to

Lausch - August 29, 2007 - 02:24

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

 
 

Drupal is a registered trademark of Dries Buytaert.