WordPress.org

Ready to get started?Download WordPress

Forums

Hit counter increased by 2 (2 posts)

  1. SpiritOfTheWinds
    Member
    Posted 1 year ago #

    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]");
    	fclose($fp);
    }
    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.

    Thanks

  2. SpiritOfTheWinds
    Member
    Posted 1 year ago #

    Ignore this thread, see http://wordpress.org/support/topic/hit-counter-increases-by-2 in the sub-forum. Thanks.

Topic Closed

This topic has been closed to new replies.

About this Topic