<?php $counterFile = "counter.txt"; clearstatcache(); if (!file_exists($counterFile)) { file_put_contents($counterFile, 0); } $fp = fopen("counter.txt", "r+"); if (flock($fp, LOCK_EX)) { // 進行排它型鎖定 $num = trim(fgets($fp,4096)); $num = intval($num); $num++; echo "您是第 " . "$num" . " 位訪客"; ftruncate($fp, 0); // truncate file fseek($fp, 0, SEEK_SET); fwrite($fp, $num); fflush($fp); // flush output before releasing the lock flock($fp, LOCK_UN); // 釋放鎖定 } else { echo "Couldn't get the lock!"; } ?>