これが私のサイトで数えるために使用するコードです。問題は、すべてのクリックがカウントされない場合があることです。
<?php
//acquire file handle
$fd=fopen('counter.txt','c+b');
if (!$fd) die("");
//lock the file - we must do this BEFORE reading, as not to read an outdated value
if (!flock($fd,LOCK_EX)) die("");
//read and sanitize the counter value
$counter=fgets($fd,10);
if ($counter===false) die("");
if (!is_numeric($counter)) {
flock($fd,LOCK_UN);
die("");
}
//increase counter and reconvert to string
$counter++;
$counter="$counter";
//Write to file
if (!rewind($fd)) {
flock($fd,LOCK_UN);
die("");
}
$num=fwrite($fd,$counter);
if ($num!=strlen($counter)) {
flock($fd,LOCK_UN);
die("");
}
//Unlock the file and close file handle
flock($fd,LOCK_UN);
fclose($fd);
?>
今何をしたらいいのかわからない。コードを書くためのより良い方法はありますか、それとも別の手法を使用する必要がありますか?