PHP counter uses ASCII file, gets reset if its 2quick

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
scr0p
Forum Newbie
Posts: 23
Joined: Mon May 05, 2003 6:49 pm
Location: NY

PHP counter uses ASCII file, gets reset if its 2quick

Post by scr0p »

http://scr0p.no-ip.org/counter.php
The problem is that it gets reset if I refresh it too much, if I hold F5 for 2 seconds and let it, itll reset, why is this? the ascii file is too slow? I will eventually use mySQL but for now, what is the problem.

Code: Select all

<?php
	$fp = fopen('counter.txt', 'r') or die('couldnt open');
	$line = fgets($fp, 1024);
	fclose($fp);
	print 'Hits: '.$line;
	
	$fp = fopen('counter.txt', 'w') or die('couldnt open to w');
	fwrite($fp, $line + 1);
	fclose($fp);
?>
Lonewolf
Forum Commoner
Posts: 33
Joined: Tue May 06, 2003 9:36 pm

Post by Lonewolf »

whats on line 31
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

seems like you're already switching to mysql ;)

assume the first request's run just executed $fp = fopen('counter.txt', 'w') or die('couldnt open to w'); but didn't advance to the next statement, the file is truncated, nothing in there. At this state a second request performs $line = fgets($fp, 1024);, $line will be empty and treated as 0 when it comes to $line + 1

If you're going to keep the flat file attempt you should read http://www.php.net/manual/en/function.flock.php
Post Reply