Page 1 of 1

Simple Text Counter - Randomly Resetting Issue

Posted: Sat Jan 14, 2012 8:52 pm
by nbasso713
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.

Code: Select all

// 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;
}


?>

Re: Simple Text Counter - Randomly Resetting Issue

Posted: Sun Jan 15, 2012 1:37 pm
by nbasso713
Okay, so I'm pretty sure the problem is when too many people are using the site at one time that the counter resets to 0. Any ideas?

Re: Simple Text Counter - Randomly Resetting Issue

Posted: Sun Jan 15, 2012 4:12 pm
by twinedev
It is most likely visitor A is hitting the site, it locks out the log file to write it,

But you are getting visitor B that is hitting the same exact time, it is trying to read the file, but Vistor A has it locked, so it is setting it to zero

Visitor B then ends up writing the new count (1) to the file.

To test this theory, change this line:

Code: Select all

$count = intval(trim(file_get_contents($logfile))) or $count = 0;
to be something like:

Code: Select all

$count = intval(trim(file_get_contents($logfile))) or $count = 8872;
If I am correct on my guess, you will see it resetting to 8872 (well 8873 since it added 1).

-Greg

Re: Simple Text Counter - Randomly Resetting Issue

Posted: Sun Jan 15, 2012 5:37 pm
by nbasso713
Thanks, that is exactly what I was thinking too. I guess i could just keep the default count more up to date until i figure something out.