我有一个脚本可以计算你访问某个页面的次数,我想要做的是让计数器在每次有效时重置5. ie(从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);
答案 0 :(得分: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);
答案 1 :(得分: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);