Support » Plugins and Hacks » Hit counter increased by 2

Hit counter increased by 2

  • Hi,

    I’m having a hard time with a hit counter implemented for a WordPress site. I have this in my functions.php (also tried in the footer directly):

    function get_counter() {
    $count_my_page = get_template_directory()."/widgets/hitcounter.txt";
    $hits = file($count_my_page);
    if (is_page_template('front-page.php')) {
    	$hits[0] ++;
    	$fp = fopen($count_my_page , "w");
    	fputs($fp , "$hits[0]");
    return $hits[0];

    I have tested with the IF condition and without. The file gets open, read, incremented for the front-page, saved and shown correctly. However, it seems to reopen and resave another time on the back-end (or where ever it is) because the value has been incremented 2 times in the file (though it displays correctly with 1 increment). I tested the code in a PHP clean installation (without WordPress) and it works fine. It seems like its something with WordPress only. Anyone noticed this behaviour?

    I can’t seem to find a solution to this. My client clearly asked for a front page view counter and I have to fix this.


Viewing 1 replies (of 1 total)
Viewing 1 replies (of 1 total)
  • The topic ‘Hit counter increased by 2’ is closed to new replies.