note 58839 deleted from function.flock by nlopess
| From: | nlopess@php.net | Date: | Thu, 08 Dec 2005 13:25:41 +0000 |
| Subject: | note 58839 deleted from function.flock by nlopess | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-99747@lists.php.net to get a copy of this message | ||
Note Submitter: dean dot shelley at gmail dot com
----
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.