note 60081 added to function.file

From: Date: Sun, 25 Dec 2005 18:08:55 +0000
Subject: note 60081 added to function.file
Groups: php.notes 
Request: Send a blank email to php-notes+get-100684@lists.php.net to get a copy of this message
I've creates this function to have my own hitcounter on my site; the function writes to a file and returns the value. Created by Mr.B <?php function hitcount() { $file = "counter.txt"; if ( !file_exists($file)){ touch ($file); $handle = fopen ($file, 'r+'); // Let's open for read and write $count = 0; } else{ $handle = fopen ($file, 'r+'); // Let's open for read and write $count = fread ($handle, filesize ($file)); settype ($count,"integer"); } rewind ($handle); // Go back to the beginning /* * Note that we don't have problems with 9 being fewer characters than * 10 because we are always incrementing, so we will always write at * least as many characters as we read **/ fwrite ($handle, ++$count); // Don't forget to increment the counter fclose ($handle); // Done return $count; } ?> ---- Manual Page -- http://www.php.net/manual/en/function.file.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+60081 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+60081&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+60081&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#100684) next »