i have prepared a php file for visitcount. that creates a text file per
day. as soon as any user visits that page ,a counter is incremented. but on
my site there is lots of trafic. that's why counter again starts with zero
. and file also written to null.b'coz multiple users(abt 1 to 10 lakh) can
visit page at same time.
so i want to know if code is right then what is the problem?
how can we solve this proble? is there any locking file mechanism in php?
code :
Expand|Select|Wrap|Line Numbers
- $count_path = "../count_visit/".date("d-m-Y").".txt";
- if(file_exists($count_path))
- {
- $handle = fopen($count_path, 'r');
- $read_count= fread($handle, filesize($count_path));
- fclose($handle);
- /* or used $read_count = file_get_contents($count_path);
- */
- if($read_count <> '' )
- {
- $inc_count = (int)$read_count + 1;
- $handle = fopen($count_path, 'w');
- fwrite($handle, $inc_count);
- fclose($handle);
- }
- }
- else
- {
- // executes when any user visits first time
- $handle = fopen($count_path, 'w');
- fwrite($handle, '1' );
- fclose($handle);
- }