This patch is based on this work:
http://wiki.erazor-zone.de/wiki:projects:php:dokuwiki:counter
New in this version against the erazor-zone.de version:
I modified the erazor-zone.de version to be used in php4 and added a few file checks.
You can contact me here: http://tinymailto.com/oliver
Steps for installing the hit counter patch:
<?php /* * Simulating PHP5's file_put_contents() function in PHP 4.3.9 (Centos 4.4) * Contact e-mail in: http://tinymailto.com/oliver */ function file_put_contents_counter($file, $views) { $written = false; if( $fp_counter = fopen($file, 'w') ) { // lock before writting if (flock($fp_counter, LOCK_EX)) { $written = fwrite($fp_counter, $views); flock($fp_counter, LOCK_UN); } fclose($fp_counter); } if($written === FALSE) { return 0; } else { return $written; } } global $ID,$ACT; // local variables $file = ""; $views = false; $read_tries = 3; $read_count = 0; $written=0; $write_tries = 3; $write_count = 0; $file=realpath($conf['datadir']).'/_views/'.$ID; if (file_exists($file)) { while ($views===FALSE) { $views=file_get_contents($file); if ($views===FALSE) { usleep(50000); } $read_count++; if($read_count > $read_tries) { $views = 0; break; } } $views = intval($views) + 1; } else { $views=1; } if (($ACT == 'show') && ($INFO['exists'])) { while(!$written) { $written=file_put_contents_counter($file,$views); // for php5 use this line //$written=file_put_contents($file,$views); if (!$written) { usleep(50000); } $write_count++; if($write_count > $write_tries) { break; } } } $fn.=" ($views views) ";
740 // print it
741 if($INFO['exists']){
+742 // counter
+743 include('counter.php');
744 print $fn;