Simple Text Counter - Randomly Resetting Issue

So i’ve been using this basic counter script on my site for a while now, and just when it started to break 25,000 i’ve noticed that it’s been randomly resetting. I’m not sure if it has to do w/ an increase in traffic or what. But the value is stored in a text file and incremented.

Any suggestions are appreciated.

[php]// Count UNIQUE visitors ONLY? 1 = YES, 0 = NO
$count_unique = 0;

// Number of hours a visitor is considered as “unique”
$unique_hours = 24;

// Minimum number of digits shown (zero-padding). Set to 0 to disable.
$min_digits = 1;

#############################

DO NOT EDIT BELOW

#############################

/* Turn error notices off */
error_reporting(E_ALL ^ E_NOTICE);

/* Get page and log file names */
$page = input($_GET[‘page’]) or die(‘ERROR: Missing page ID’);
$logfile = ‘logs/’ . $page . ‘.txt’;

/* Does the log exist? /
if (file_exists($logfile))
{
/
Get current count */
$count = intval(trim(file_get_contents($logfile))) or $count = 0;
$english_format_number = number_format($count);
$cname = ‘tcount_unique_’.$page;

if ($count_unique==0 || !isset($_COOKIE[$cname]))
{
	/* Increase the count by 1 */
	$count = $count + 1;
	$fp = @fopen($logfile,'w+') or die('ERROR: Can\'t write to the log file ('.$logfile.'), please make sure this file exists and is CHMOD to 666 (rw-rw-rw-)!');
	flock($fp, LOCK_EX);
	fputs($fp, $count);
	flock($fp, LOCK_UN);
	fclose($fp);

	/* Print the Cookie and P3P compact privacy policy */
	header('P3P: CP="NOI NID"');
	setcookie($cname, 1, time()+60*60*$unique_hours);
}

/* Is zero-padding enabled? */
if ($min_digits > 0)
{
    $count = sprintf('%0'.$min_digits.'s',$count);
}

/* Print out Javascript code and exit */
echo 'document.write(\''.number_format($count).'\');';
exit();

}
else
{
die(‘ERROR: Invalid log file!’);
}

/* This functin handles input parameters making sure nothing dangerous is passed in */
function input($in)
{
$out = htmlentities(stripslashes($in));
$out = str_replace(array(’/’,’\’), ‘’, $out);
return $out;
}

?>[/php]

Have you ever seen the value close to it resetting?

And when it resets, does the text file become blank or have the value of 0 put in it?

I’m pretty sure it’s caused by the text file not be accessible because it’s being called simultaneously by multiple users, but it’s already in use. I think I’m just going to take the text file out of the equation and use a database.

I think that would be a good idea. You could also then store other information about the data - e.g. a timestamp, IP etc and then use this data to see how many users have visited over a long period of time.

In addition to this - if you’re looking for a more advanced stats solution - you could use a service (such as Google Analytics) or a script (e.g. Piwik).

Thanks. I wrote a script last night utilizing a database and it works great.

nbasso, I am having the same problem with the same script. Would you mind pointing me in the right direction to do what you did?

Thank you.

I just created a new table in my sites database to store the count number which is an integer. Following the php code that has all my database info I use:

[php]mysql_query(“UPDATE counter SET counter = counter + 1”); //adds +1 to the current count[/php]

Then in the body of my page where I wanted my counter to be displayed I set the count to a variable and simply print it:

[php]$count = mysql_fetch_row(mysql_query(“SELECT counter FROM counter”));[/php]

Hopefully these two key bits will help you out. Unfortunately I don’t know much PHP at the moment, but getting this setup was pretty straightforward.

Perfect, thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service