How to not count views made by the Gallery 3 Admin

If you’re like me, you like to know how many times your photos are viewed; however, you don’t want to count the views made by yourself because who cares how many times you look at one of your own images?

Here’s how I got Gallery 3.0 to stop counting my views as the gallery administrator. (Update 1-27-2011: For those that have upgraded to Gallery 3.0.1, please see this post.) I should mention that my target audience for this post are those that are familiar with editing PHP files and have a means to access their Gallery database (to reset the view_counts back to zero).

First, in /themes/(your theme)/views/page.html.php add the following code near the top (below the first line, making sure you keep the code within PHP opening and closing tags.):

2
3
4
5
6
7
8
<?
if ($user->admin == '1') {
   $_SESSION['admin'] = true;
} else {
   $_SESSION['admin'] = false;
}
?>

Then, in /modules/gallery/controllers/photos.php modify the code around line 50: (make sure you do not leave the existing view_count++ statement outside the IF statements; otherwise, you’ll get duplicate view counts)

50
51
52
53
54
55
56
    $template->content = new View("photo.html");

    // mrh added the session admin check
    if(!$_SESSION['admin']) {
      $photo->view_count++;
    }
    $photo->save();

Then, in /modules/gallery/controllers/movies.php around line 50:

50
51
52
53
54
55
56
57
58
    $template->content = new View("movie.html");

    // mrh added the session admin check
    if (!$_SESSION['admin']) {
      $movie->view_count++;
    }
    $movie->save();

    print $template;

Finally, in /modules/gallery/controllers/albums.php around line 77:

77
78
79
80
81
82
83
    // mrh added the session admin check
    if (!$_SESSION['admin']) {
      db::query("UPDATE {items} SET `view_count` = `view_count` + 1 WHERE `id` = $album->id")
      ->execute();
    }

    print $template;

The Gallery will now not count any views made by the administrator which in my case, is me. πŸ™‚ To reset the view counts back to zero, the following SQL statement will work:

UPDATE items SET view_count = 0 WHERE view_count > 0;

Since you’re modifying the core Gallery code, be sure to make backups before applying any future upgrades as your changes will be lost.

Hopefully this helps! πŸ™‚

8 thoughts on “How to not count views made by the Gallery 3 Admin

  1. Uwe

    Hi Mark,

    thanks for your tip. Unfortunatly, it doesn’t work with my gallery. if I change the page.html.php the code will be screened at the top of the gallery-view. When I changed the other php-files, the gallery isn’t accessible, a white page displayed. Do you have any idea?

    regards
    Uwe

    Reply
  2. MarkRH Post author

    Make sure when you add the code to the top of the page.html.php file that you keep it within opening and closing PHP tags:

    <?php defined("SYSPATH") or die("No direct script access.");
    if ($user->admin == '1') {
       $_SESSION['admin'] = true;
    } else {
       $_SESSION['admin'] = false;
    }
    ?>

    The white page usually means there’s a PHP error of some kind occurring. My guess is that you might not have added a semicolon “;” to the end of the first line in the code above. If memory serves, it did not originally have one. Since Gallery has error reporting turned off by default, might want to look at this: http://codex.gallery2.org/Gallery3:FAQ#How_do_I_see_debug_information.3F

    Hope that helps,
    Mark H.

    Reply
  3. Uwe

    Hi Mark,

    thanks for your friendly help. I think this little sign ‘;’ was the mistake. Now the gallery ist availaible, no white page will display.

    Unfortunally, the counter still works, even though I’m not logged in??

    best regards
    Uwe

    Reply
  4. MarkRH Post author

    Put the following at the end of your page.html.php file just above the closing BODY tag:

    <?       echo 'session admin: '.$_SESSION['admin'].'<br />';  ?>

    If the value is set correctly, it should show a value of 1. If it does then you’ve made some mistake editing the other files. Either the statement increasing the view count is not inside the if statement or some other typo was made is all I can think of at the moment.

    Reply
  5. Uwe

    Hi mark,

    thanks a lot for your help.
    At this time, it counts one more, when I visit an album (when I’m logged in). When I’m logged out,
    the click to an album count 2 visits.

    I’ve checked all edits I’v made, but I can’t find any mistake. So I give up.

    But again, thanks for your help.

    best regards
    Uwe

    Reply
  6. MarkRH Post author

    I know what’s happening now. You left the existing view_count++ statements in the code. Those statements should only appear inside the IF statements that you added. This explains why it counts two times when your not logged in and only once when you are.

    The statement that increases the count should ONLY appear within the IF statement. πŸ™‚

    Hope that finally fixes it for you.

    Reply
  7. Uwe

    Okay…. I checked it.

    in photo.php the view_count++ statements is in the code, so when I view a photo with the admin account, the count will not be increased.

    But in the album-view it will count my clicks when I’m logged in as the admin. I copied the code of your tip with the clipboard in my file. In album.php isn’t the view_count++ statements.

    Is it possible to send my files to you?

    I would be grateful if you could have a look directly in my modified files…

    regrads
    Uwe

    Reply