Location: PHPKode > scripts > KubeStats > statlog.php
<?
require_once('includes/config.php');
require_once('includes/functions/func.global.php');

session_start();

if(!isset($_GET['id']))
{
	exit;
}
else
{
	if(!$_GET['id'])
	{
		exit;
	}
	else
	{
		$site_id = $_GET['id'];
	}
}

db_connect($config);

$day = date("j");
$month = date("n");
$year = date("Y");
$unique = true;
$ip = real_ip($_SERVER,$_ENV);
$host = gethostbyaddr($ip);
$viewing_page = $_SERVER['HTTP_REFERER'];
$agent = $_SERVER['HTTP_USER_AGENT'];

if(isset($_GET['refer']))
{
	if(($_GET['refer'] == 'blockedReferrer') OR ($_GET['refer'] == 'http://blockedReferrer'))
	{
		$refer = '';
	}
	else
	{
		$refer = $_GET['refer'];
	}
}
else
{
	$refer = '';
}

if($refer)
{
	$refer_parts = parse_url($refer);
	
	$refer_page = str_replace('http://www.','',$refer);
	$refer_page = str_replace('http://','',$refer_page);
	$refer_page = 'http://'.$refer_page;
	
	$refer_id = 0;
	
	$query = "SELECT refer_id FROM ".$config['db']['pre']."referer WHERE site_id='".addslashes($site_id)."' AND url='".addslashes($refer_page)."' LIMIT 1";
	$query_result = mysql_query($query);
	while ($info = @mysql_fetch_array($query_result))
	{
		$refer_id = $info['refer_id'];
	}
	
	if($refer_id == 0)
	{
		mysql_query("INSERT INTO `".$config['db']['pre']."referer` ( `site_id` , `url` , `count` ) VALUES ('".addslashes($site_id)."', '".addslashes($refer_page)."', '1');");
	}
	else
	{
		mysql_query("UPDATE `".$config['db']['pre']."referer` SET `count` = `count`+1 WHERE `refer_id` =".$refer_id." AND `site_id` =".addslashes($site_id)." LIMIT 1 ;");
	}
	
	if($refer_parts['host'] == 'search.msn.com')
	{
		$q_parts = explode('&',$refer_parts['query']);
		
		foreach ($q_parts as $key => $value)
		{
			$thisone = explode('=',$value);
			
			if($thisone[0] == 'q')
			{
				$query = urldecode($thisone[1]);
				
				mysql_query("INSERT INTO `".$config['db']['pre']."searches` ( `site_id` , `search_word` , `search_engine` , `search_count` ) VALUES ('".$site_id."', '".addslashes($query)."', '0', '0');");
				mysql_query("UPDATE `".$config['db']['pre']."searches` SET `search_count` = search_count+1 WHERE `search_word` ='".addslashes($query)."' AND search_engine='0'  AND `site_id` =".$site_id." LIMIT 1;");
			
				break;
			}
		}
	}
	elseif(eregi('google.',$refer_parts['host']))
	{			
		$q_parts = explode('&',$refer_parts['query']);
		
		foreach ($q_parts as $key => $value)
		{
			$thisone = explode('=',$value);
			
			if($thisone[0] == 'q')
			{
				$query = urldecode($thisone[1]);
				
				mysql_query("INSERT INTO `".$config['db']['pre']."searches` ( `site_id` , `search_word` , `search_engine` , `search_count` ) VALUES ('".$site_id."', '".addslashes($query)."', '1', '0');");
				mysql_query("UPDATE `".$config['db']['pre']."searches` SET `search_count` = search_count+1 WHERE `search_word` ='".addslashes($query)."' AND search_engine='1' AND `site_id` =".$site_id." LIMIT 1;");
			
				break;
			}
		}
	}
	elseif($refer_parts['host'] == 'search.yahoo.com')
	{
		$q_parts = explode('&',$refer_parts['query']);
		
		foreach ($q_parts as $key => $value)
		{
			$thisone = explode('=',$value);
			
			if($thisone[0] == 'p')
			{
				$query = urldecode($thisone[1]);
				
				mysql_query("INSERT INTO `".$config['db']['pre']."searches` ( `site_id` , `search_word` , `search_engine` , `search_count` ) VALUES ('".$site_id."', '".addslashes($query)."', '2', '0');");
				mysql_query("UPDATE `".$config['db']['pre']."searches` SET `search_count` = search_count+1 WHERE `search_word` ='".addslashes($query)."' AND search_engine='2'  AND `site_id` =".$site_id." LIMIT 1;");
			
				break;
			}
		}
	}
	elseif($refer_parts['host'] == 'search.live.com')
	{
		$q_parts = explode('&',$refer_parts['query']);
		
		foreach ($q_parts as $key => $value)
		{
			$thisone = explode('=',$value);
			
			if($thisone[0] == 'q')
			{
				$query = urldecode($thisone[1]);
				
				mysql_query("INSERT INTO `".$config['db']['pre']."searches` ( `site_id` , `search_word` , `search_engine` , `search_count` ) VALUES ('".$site_id."', '".addslashes($query)."', '3', '0');");
				mysql_query("UPDATE `".$config['db']['pre']."searches` SET `search_count` = search_count+1 WHERE `search_word` ='".addslashes($query)."' AND search_engine='3'  AND `site_id` =".$site_id." LIMIT 1;");
			
				break;
			}
		}
	}
}

if(isset($_COOKIE['stat_'.$site_id]))
{
	if(($_COOKIE['stat_'.$site_id]+1800) > time())
	{
		$unique = false;
	}
}

if($unique)
{
	// Get the browser info
	$browser = get_browser_info($agent);
	
	$browserlookup = mysql_num_rows(mysql_query("SELECT 1 FROM stat_browsers WHERE browser_id='".$browser['browser_id']."' AND site_id='".addslashes($site_id)."' LIMIT 1"));
	
	if($browserlookup)
	{
		mysql_query("UPDATE `stat_browsers` SET `count` = count+1 WHERE `browser_id` =".$browser['browser_id']." AND `site_id` =".addslashes($site_id)." LIMIT 1 ;");
	}
	else
	{
		mysql_query("INSERT INTO `stat_browsers` ( `browser_id` , `site_id` , `count` ) VALUES ('".$browser['browser_id']."', '".addslashes($site_id)."', '1');");
	}
}

$lookupday = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."days WHERE site_id='".addslashes($site_id)."' AND day='".$day."' AND month='".$month."' AND year='".$year."' LIMIT 1"));

if($lookupday)
{
	if($unique == false)
	{
		mysql_query("UPDATE `".$config['db']['pre']."days` SET `visits` = visits+1 WHERE `day` =".$day." AND `month` =".$month." AND `year` =".$year." AND `site_id` =".addslashes($site_id)." LIMIT 1;");
		mysql_query("UPDATE `".$config['db']['pre']."months` SET `visits` = visits+1 WHERE `month` =".$month." AND `year` =".$year." AND `site_id` =".addslashes($site_id)." LIMIT 1;");
	}
	else
	{
		mysql_query("UPDATE `".$config['db']['pre']."days` SET `visits` = visits+1,`unique` = `unique`+1 WHERE `day` =".$day." AND `month` =".$month." AND `year` =".$year." AND `site_id` =".addslashes($site_id)." LIMIT 1;");
		mysql_query("UPDATE `".$config['db']['pre']."months` SET `visits` = visits+1,`unique` = `unique`+1 WHERE `month` =".$month." AND `year` =".$year." AND `site_id` =".addslashes($site_id)." LIMIT 1;");
	}
}
else
{
	mysql_query("INSERT INTO `".$config['db']['pre']."days` ( `site_id` , `day` , `month` , `year` , `stamp` , `visits` , `unique` ) VALUES ('".addslashes($site_id)."', '".$day."', '".$month."', '".$year."', '".strtotime("today")."', '1', '1');");
	$lookupmonth = mysql_num_rows(mysql_query("SELECT 1 FROM ".$config['db']['pre']."months WHERE site_id='".addslashes($site_id)."' month='".$month."' AND year'".$year."' LIMIT 1"));
	
	if($lookupmonth)
	{
		mysql_query("UPDATE `".$config['db']['pre']."months` SET `visits` = visits+1 WHERE `month` =".$month." AND `year` =".$year." AND `site_id` =".addslashes($site_id)." LIMIT 1;");
	}
	else
	{
		mysql_query("INSERT INTO `".$config['db']['pre']."months` ( `site_id` , `month` , `year` , `visits` , `unique` ) VALUES ('".addslashes($site_id)."','".$month."','".$year."', '1', '1');");
	}
}

setcookie('stat_'.$site_id, time(), time()+31536000);

mysql_query("INSERT INTO `".$config['db']['pre']."recent` ( `recent_id` , `site_id` , `time` , `ip` , `host` , `referrer` , `page` ) VALUES ('', '".addslashes($_GET['id'])."', '".time()."', '".addslashes($ip)."', '".addslashes($host)."', '".addslashes($refer_page)."', '".addslashes($viewing_page)."');");

if(!isset($_GET['refer']))
{
	header("Content-type: image/gif");
	header("Content-Length: 43");
		
	$fp = fopen('images/spacer.gif', 'rb');
	fpassthru($fp);
}
?>
Return current item: KubeStats