Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $myFile = "varCompteur.txt";
- //Obtien le chiffre du fichier;
- $fh = fopen($myFile,'r');
- $cpt = 0;
- $theNumber = fread($fh,filesize($myFile));
- $nb = $theNumber;
- $nb = $nb + 1;
- fclose($fh);
- // waiting until file will be locked for writing (1000 milliseconds as timeout)
- if ($fp = fopen($myFile, 'a')) {
- $startTime = microtime();
- $canWrite = false;
- while (!$canWrite):
- $canWrite = flock($fp, LOCK_EX);
- // If lock not obtained sleep for 0 - 100 milliseconds, to avoid collision and CPU load
- if(!$canWrite){usleep(round(rand(0, 100)*1000));}
- endwhile;
- //file was locked so now we can store information
- if ($canWrite) {
- ftruncate($fp, 0);
- fwrite($fp, $nb);
- }
- fclose($fp);
- }
- echo "La page à été chargée $nb fois";
- ?>
Add Comment
Please, Sign In to add comment