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

两盒软妹~` 提交于 2019-11-26 23:40:40

问题


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

<?php
$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+");
    if($fh==false)
        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);
    fclose($fh);

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

回答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+");
    if(!$fh)
        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.
    fclose($fh);
    echo '------------------------------',
         '<br />Site Wide Page Views: ',
         $total_site_page_loads,
         '<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.)




回答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?



来源:https://stackoverflow.com/questions/5250118/how-to-keep-this-counter-from-reseting-at-100-000

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!