0

ページにアクセスした回数をカウントするスクリプトがあります。私がやりたいことは、カウンターが 5 になるたびにリセットすることです。

$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 に答える