4images Forum & Community

4images Modifications / Modifikationen => Mods & Plugins (Requests & Discussions) => Topic started by: alekseyn1 on December 01, 2009, 02:58:23 PM

Title: Image views count based on cookies or IP
Post by: alekseyn1 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?
Title: Re: Image views count based on cookies or IP
Post by: V@no 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).
Title: Re: Image views count based on cookies or IP
Post by: alekseyn1 on December 02, 2009, 12:16:04 PM
Great! works perfectly!
Thanks very much!