note 58839 added to function.flock

From: Date: Thu, 17 Nov 2005 12:40:38 +0000
Subject: note 58839 added to function.flock
Groups: php.notes 
Request: Send a blank email to php-notes+get-98705@lists.php.net to get a copy of this message
If anyone is using this feature for a topsite script etc where you are counting hits, it may seem obvious but what i would do is append a dot to a file everytime the site is visited. That way you are only writing 1 byte of data to the file, so you have less chance of it being interrupted if you write a minimal amount of information to the file. Then use filesize to get the total amount of data written to that file: <?php // Location of your data file (for storing hits) $file = '/path/to/file.txt'; // Open file with using 'a' (append) method, if it isn't there // php will create it if the chmod settings of dir is 777. // Also append a dot to it all on the same line fwrite ( fopen ( $file, 'a+' ), '.' ); // Close the file to allow other processes to use it fclose ( $file ); // Assign the count var with the size in bytes of the data file $count = filesize ( $file ); ?> I'm still learning, but i found this really reliable as opposed to opening up a file, reading the contents, increasing the number inside the file by one and then closing it. If there are any problems with this, or you just disagree please follow up or email me. ---- Manual Page -- http://www.php.net/manual/en/function.flock.php Edit -- http://master.php.net/manage/user-notes.php?action=edit+58839 Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes&reason=added+to+the+manual Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes&reason=bad+code Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes&reason=spam Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes&reason=useless Delete: non-english -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes&reason=non-english Delete: already in docs -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes&reason=already+in+docs Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+58839&report=yes Reject -- http://master.php.net/manage/user-notes.php?action=reject+58839&report=yes Search -- http://master.php.net/manage/user-notes.php

« previous php.notes (#98705) next »