我用这个来计算我网站的点击量:
$counter_name = "counter.txt";
$f = fopen($counter_name,"r");
$counterVal = fread($f, filesize($counter_name));
fclose($f);
$counterVal++;
$f = fopen($counter_name, "w");
fwrite($f, $counterVal);
fclose($f);
我想问一下,如果有2个或更多的访问者同时打开页面,会不会有问题?如果是,如何计算所有命中?
当(例如)100个用户同时打开页面时,文件不会被其中一个锁定吗?
非常感谢!
这可能对你有帮助:
$filename = "counter.txt";
$number = file_get_contents($filename);
$file = fopen($filename, 'a');
if (flock($file, LOCK_EX)) {
ftruncate($file, 0);
fwrite($file, $number+1);
flock($file, LOCK_UN);
}
fclose($file);