• Advertisement
Sign in to follow this  

[web] Accurrate counter?

This topic is 4732 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement