How to keep this counter from reseting at 100,000?

This script resets after 100,000. What do I need to change to prevent a reset and instead keep counting?

$filename1 = 'content/general_site_data/total_site_page_loads.txt';

if (file_exists($filename1)) {
    $fh = fopen("content/general_site_data/total_site_page_loads.txt", "a+");
        die("unable to create file");

    $filec = 'content/general_site_data/total_site_page_loads.txt';
    if (!is_writable($filec))
        die('not writable');

    $total_site_page_loads = trim(file_get_contents($filec)) + 1;
    fwrite(fopen($filec, 'w'), $total_site_page_loads);

    echo '------------------------------<br />
    Site Wide Page Views: '.$total_site_page_loads.'<br />';
} else {
    $fh = fopen($filename1, "a");
    $total_site_page_loads = trim(file_get_contents($filename1)) + 1;
    fwrite($fh, $total_site_page_loads);

    echo '------------------------------<br />
    Site Wide Page Views: '.$total_site_page_loads.'<br />';

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

Your code may be suffering from a race condition.

Mid way through, you re-open the file in w mode, which truncates the file to zero length. If another copy of your script opens and attempts to read the file while it’s been truncated but before it’s been read, the counter will be reset to zero.

Here is an updated version of your code:

    $filename = 'content/general_site_data/total_site_page_loads.txt';
// Open our file in append-or-create mode.
    $fh = fopen($filename, "a+");
        die("unable to create file");
// Before doing anything else, get an exclusive lock on the file.
// This will prevent anybody else from reading or writing to it.
    flock($fh, LOCK_EX);
// Place the pointer at the start of the file.
    fseek($fh, 0);
// Read one line from the file, then increment the number.
// There should only ever be one line.
    $total_site_page_loads = 1 + intval(trim(fgets($fh)));
// Now we can reset the pointer again, and truncate the file to zero length.
    fseek($fh, 0);
    ftruncate($fh, 0);
// Now we can write out our line.
    fwrite($fh, $total_site_page_loads . "\n");
// And we're done.  Closing the file will also release the lock.
    echo '------------------------------',
         '<br />Site Wide Page Views: ',
         '<br />';

Because the initial open is in append-or-create mode, you don’t need to handle a case where the file doesn’t exist, unless the initial open failed.

With the file locking in place, this code should never reset the counter in the file, no matter how many concurrent requests there are. (Unless you happen to also have other code writing to the file, of course.)

Solution 2

I can’t see where any reset would occur but how the script works seems pretty straightforward. Maybe try editing total_site_page_loads.txt to something like 99990 and watch what happens to that file as you cross over to 100000?

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply