0

我有一个脚本可以计算您访问页面的次数,我想做的是让计数器在它达到 5 时重置。即(从 1-5 计数)并将重置回 1。有什么想法吗?

$handle = fopen('counter.txt', 'r+');

    flock($handle, LOCK_EX);

        $total = (int) fread($handle, max(1, filesize('counter.txt')));

        $newTotal = ++$total;

        rewind($handle);

        fwrite($handle, $newTotal);

fclose($handle);
4

2 回答 2

1

很简单,

您可以使用

    $handle = fopen('counter.txt', 'r+');

    flock($handle, LOCK_EX);

        $total = (int) fread($handle, max(1, filesize('counter.txt')));
        if($total==5){
            fwrite($handle, 1);
        }
        else{
        $newTotal = ++$total;

        rewind($handle);

        fwrite($handle, $newTotal);
        }
fclose($f);
于 2012-09-26T11:59:10.030 回答
0
$f = fopen('counter.txt', 'r+');
flock($f, LOCK_EX);
$total = (int) fread($f, max(1, filesize('counter.txt')));

if (isset($_POST['submit'])) {
    rewind($f);
    $total = ($total >= 5)? 1 : $total++; // Increment or reset
    fwrite($f, $total); // Write new value
}

fclose($f);
于 2012-09-26T12:02:40.587 回答