如何防止此计数器重置为100,000? [英] How to keep this counter from reseting at 100,000?

查看:91
本文介绍了如何防止此计数器重置为100,000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此脚本在100,000后重置.我需要更改什么以防止重置而继续计数?

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 />';
}
?>

推荐答案

您的代码可能正遭受种族条件.

途中,您将以w模式重新打开文件,该模式会将文件截断为零长度.如果脚本的另一个副本打开并尝试在文件被截断后但尚未读取之前读取该文件,则计数器将重置为零.

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.)

这篇关于如何防止此计数器重置为100,000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆