Author Topic: Image views count based on cookies or IP  (Read 4106 times)

0 Members and 1 Guest are viewing this topic.

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
Image views count based on cookies or IP
« on: December 01, 2009, 02:58:23 PM »
The trouble is by just refreshing the page with an image, the counter of views keeps adding up...
this is not always good, because if you run a proper large photo community site with ratings being important, then, it's pretty easy to get to the top of the charts as the most viewed image.

Any ideas on how to fix that?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: Image views count based on cookies or IP
« Reply #1 on: December 02, 2009, 10:16:42 AM »
Try this:
in details.php find:
  $sql "UPDATE ".IMAGES_TABLE."
          SET image_hits = image_hits + 1
          WHERE image_id = 
$image_id";
  
$site_db->query($sql);


Replace it with:
  $last_image_views explode(" "trim($site_sess->get_session_var("last_views")));
  
$last_image_views_array = array();
  
$update 0;
  foreach(
$last_image_views as $val)
    if (
$data explode("|"$val))
      if (
$data[1] + 600 time())
        
$last_image_views_array[$data[0]] = $data[0] . "|" $data[1];
      else
        
$update 1;

  if (!isset(
$last_image_views_array[$image_id]))
  {
    
$sql "UPDATE ".IMAGES_TABLE."
            SET image_hits = image_hits + 1
            WHERE image_id = 
$image_id";
    
$site_db->query($sql);
    
$last_image_views_array[$image_id] = $image_id "|"time();
    
$update 1;
  }
  if (
$update)
  {
    
$site_sess->set_session_var("last_views"implode(" "array_values($last_image_views_array)));
  }



The number 600 is 600 seconds (10 minutes). That is timeout at which visitor's hits will not be count on the same image(s).
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline alekseyn1

  • Full Member
  • ***
  • Posts: 102
    • View Profile
    • My Project
Re: Image views count based on cookies or IP
« Reply #2 on: December 02, 2009, 12:16:04 PM »
Great! works perfectly!
Thanks very much!