[web] Accurrate counter?

Started by
-1 comments, last by chadmv 19 years, 1 month ago
I coded up a counter on my site and I'm not sure if it's accurate or not. I want to keep track of total hits and hits for the current day. I subtract the interval from now() because the servers are stored 3 hours ahead of me. Is this an accurate counter?
// Increment the counter
if ( !isset( $_COOKIE['hit'] ) )
{
    setcookie( 'hit', 'counted', time() + 86400 );
	$result = $db->Query( "SELECT DATE_FORMAT( todaydate, '%Y%m%d') AS last, DATE_FORMAT( now() - INTERVAL 3 HOUR, '%Y%m%d' ) AS now FROM cu_mainsettings" );
	$row = $db->FetchArray( $result );
	if ( $row['last'] < $row['now'] )
	{
	    // New day
		$db->Query( "UPDATE cu_mainsettings SET todaycount = '1', todaydate = (now() - INTERVAL 3 HOUR)" );
	}
	else
	{
	    // Same day
		$db->Query( "UPDATE cu_mainsettings SET todaycount = (todaycount + 1)" );
	}
    $db->Query( "UPDATE cu_mainsettings SET count = (count + 1)" );
}
else
{
	// There is a cookie so check if todaycount needs to be reset
	$result = $db->Query( "SELECT DATE_FORMAT( todaydate, '%Y%m%d') AS last, DATE_FORMAT( now() - INTERVAL 3 HOUR, '%Y%m%d' ) AS now FROM cu_mainsettings" );
	$row = $db->FetchArray( $result );
	if ( $row['last'] < $row['now'] )
	{
	    // New day
		$db->Query( "UPDATE cu_mainsettings SET todaycount = '0', todaydate = (now() - INTERVAL 3 HOUR)" );
	}
	else
	{
	    // Do nothing
	}
}

This topic is closed to new replies.

Advertisement