incrementing data in txt file - Linux & windows

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

incrementing data in txt file - Linux & windows

Post by psmshankar »

Dear All,
my code to increment the page visit count works fine in windows.i did the same in linux, given the appopriate rights to counter.txt file where i am writing the count value... it actually counts but sometimes it counts high..like it increments by 3 or 2.... i donno why
in windows its perfect..wheever i refresh the page count increases by 1, whereas in linux box, sometimes it goes off to high value say from 97 to 104 somethinh like that..

Code: Select all

if (file_exists($COUNT_FILE)) 
{
	// Open, read, increment, save and close file.
	$fp = fopen("$COUNT_FILE", "r+");
	flock($fp, 1);
	$count = fgets($fp, 4096);
	//echo "<br> count = $count";
	$count += 1; 
	//echo "<br> count = $count";
	fseek($fp,0);
	fputs($fp, $count);
	flock($fp, 3);
	fclose($fp);
&#125;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Have you done debugging by echoing each variable in the script to see what their up to?

Mac
psmshankar
Forum Commoner
Posts: 96
Joined: Tue Aug 06, 2002 4:25 am
Location: India

Post by psmshankar »

yes.. i chked the count variable and displayed..sometimes its ok..sometimes its incrementing more...
is it becos that that page is accessed by others as well at that time????
thats y i am seeing and increase in the count..???? witihin that short span of time??
Post Reply