... [Meter] ... Version 1.3, 2008-03-28, http://alexandre.alapetite.fr/doc-alex/compteur/ ------------------------------------------------------------------ Written by Alexandre Alapetite, http://alexandre.alapetite.fr/cv/ Copyright 2005-2008, Licence: Creative Commons "Attribution-ShareAlike 2.0 France" BY-SA (FR), http://creativecommons.org/licenses/by-sa/2.0/fr/ http://alexandre.alapetite.fr/divers/apropos/#by-sa - Attribution. You must give the original author credit - Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one (Can be included in GPL/LGPL projects) - The French law is authoritative - Any of these conditions can be waived if you get permission from Alexandre Alapetite - Please send to Alexandre Alapetite the modifications you make, in order to improve this file for the benefit of everybody If you want to distribute this code, please do it as a link to: http://alexandre.alapetite.fr/doc-alex/compteur/ */ $session=1800; //In seconds $fmeter='meter.txt'; error_reporting(E_ERROR); header('Content-type: image/png'); require_once('http-conditional.php'); //Get this library on http://alexandre.alapetite.fr/doc-alex/php-http-304/ $begin=time(); $lastModif=max($begin-$session,filemtime($fmeter)); if (httpConditional($lastModif,$session,0,true,true)) exit(); //Cache 30 minutes, private, compression ignore_user_abort(true); // $mutexFile1='mutex1.txt'; //File to use for mutex method#1 $mutexFile2='mutex2.txt'; //File to use for mutex method#2 $mutexOK=false; $mutexMethod=0; if (file_exists($mutexFile1)) {//Mutex method#1 (best, but does not work on many systems) //Tested OK on (WinXP/NTFS/PHP5-CGI/IIS) //Tested not OK on (WinXP/FAT32/PHP5) $mutexMethod=1; $fp=fopen($mutexFile1,'w+'); $mutexOK=flock($fp,LOCK_EX); } else {//Mutex method#2 (assumes fopen() is atomic) $mutexMethod=2; if (file_exists($mutexFile2)&&($begin-filemtime($mutexFile2)>30)) {//In case a previous old execution failed to clean the mutex file. //Resistant on concurrent processes @chmod($mutexFile2,0666); unlink($mutexFile2); } $attempt=1; while (($attempt<50)&&(@fopen($mutexFile2,'x')===false)) { $begin2=time(); usleep($attempt*5000); //Sleep in milliseconds $usleep2=1000000; while (($usleep2>0)&&(time()-$begin2>$attempt*5000)) $usleep2--; //If usleep() does not work $attempt++; } $mutexOK=($attempt<50); } // if ($mutexOK) { if($myMeter=fopen($fmeter,'r')) {//Read curent meter $meter=trim(fgets($myMeter,12)); $lastIP=trim(fgets($myMeter,36)); fclose($myMeter); if (!is_numeric($meter)) $meter=0; } else { $meter=0; $lastIP=md5('127.0.0.0'); } $IP=empty($_SERVER['HTTP_X_REAL_IP']) ? $_SERVER['REMOTE_ADDR'] : $_SERVER['HTTP_X_REAL_IP']; $IP=md5($IP); if ((($begin-$clientCacheDate)>$session)&& !(($clientCacheDate<632003440)&&($IP==$lastIP))) //Small protection against multiple refresh {//If the session has expired, increment the meter $meter++; if($myMeter=fopen($fmeter,'w')) { fputs($myMeter,$meter."\n"); fputs($myMeter,$IP."\n"); fclose($myMeter); } clearstatcache(); //Update HTTP headers with new "Last-Modified" and "ETag" httpConditionalRefresh(filemtime($fmeter)); } } else $meter='?'; // switch ($mutexMethod) { case 1: flock($fp,LOCK_UN); fclose($fp); break; case 2: @chmod($mutexFile2,0666); unlink($mutexFile2); break; } // $font=5; $im=imagecreate(5+2*$font*strlen($meter),18); $bg=imagecolorallocate($im,255,255,255); imagecolortransparent($im,$bg); $tc=imagecolorallocate($im,0,0,0); imagestring($im,$font,4,2,$meter,$tc); imagepng($im); imagedestroy($im); ?>