Location: PHPKode > projects > phpCAMALEO > common/private/Stat_mngr.php
<?php
/***************************************************************************
*	Stat_mngr.php
*
*	begin		: June 2004
*	version		: 10 October 2006
*	copyright	: (C) 2004,2006 gr&&olini.net
*
*	Functions to handle site statsistics
*
*	Each kind of stat is managed pending on its switch in the configuration
*
***************************************************************************/
if($_SESSION[misc][skin]!='')
{
	if($USER_ID!='') { $stat_user=$USER_ID; } else { $stat_user=$_SESSION[id]; }
	$stat_page		=$_SESSION[PageNr];
	$stat_date		=date('Y-m-d');
	$stat_language	=$_SESSION[country_code];
	$stat_timestamp =time();
	$stat_skin=$_SESSION[misc][skin];
	#
	#==================#
	#        IP        #
	#==================#
	#
	if($_SESSION[misc][stat_ip]==1)
	{
		if(!isset($_SESSION[stat_ip_ok]) || $_SESSION[stat_ip_ok]!='done')
		{
			#	echo 'stat_ip='.$IP.'<hr>';#debug
			#	Do it only once per session!
			#
			$_SESSION[stat_ip_ok]='done';
			$sth=db_query("SELECT * FROM g_stat_ip WHERE stat_ip_skin='$stat_skin' AND stat_ip_ip='$IP' AND stat_ip_date='$stat_date'");
			if($sth[1]==0) { $sth=db_query("INSERT INTO g_stat_ip SET stat_ip_skin='$stat_skin', stat_ip_ip='$IP', stat_ip_date='$stat_date', stat_ip_country='$stat_language'"); }
		}
	}
	$stat_visit_unique=0; $stat_visit_reload=0; $stat_visit_visitors=0;
	#
	#==================#
	#     Language     #
	#==================#
	#
	if($_SESSION[misc][stat_lang]==1)
	{
		if(!isset($_SESSION[stat_language_ok]) || $_SESSION[stat_language_ok]!='done')
		{
			#	echo 'stat_lang='.$stat_language.'<hr>';#debug
			#	Do it only once per session!
			#
			$_SESSION[stat_language_ok]='done';
			$sth=db_query("SELECT * FROM g_stat_lang WHERE stat_lang_skin='$stat_skin' AND stat_lang_lang='$stat_language' AND stat_lang_date='$stat_date'"); $op_type='INS';
			if($sth[1]>0) { $op_type='UPD'; $row=db_fetch($sth[0]); $stat_visit_visitors=$row[0][stat_lang_visits]; }
			#
			$args="SELECT DISTINCT stat_online_date, stat_online_user, stat_online_ip FROM g_stat_online WHERE stat_online_skin='$stat_skin' AND stat_online_date='$stat_date' AND stat_online_user='$stat_user'";
			if(CheckUserIDOnline($args)=='unique' || $stat_visit_visitors==0) { $stat_visit_visitors++; }
			#
			switch($op_type)
			{
				case 'INS':	$sth=db_query("INSERT INTO g_stat_lang SET stat_lang_skin='$stat_skin', stat_lang_lang='$stat_language', stat_lang_date='$stat_date', stat_lang_visits='$stat_visit_visitors'");	break;
				case 'UPD':	$sth=db_query("UPDATE g_stat_lang SET stat_lang_lang='$stat_language', stat_lang_date='$stat_date', stat_lang_visits='$stat_visit_visitors' WHERE stat_lang_skin='$stat_skin' AND stat_lang_lang='$stat_language' AND stat_lang_date='$stat_date' LIMIT 1"); break;
			}
		}
	}
	#
	#==================#
	#      Pages       #
	#==================#
	#
	if($_SESSION[misc][stat_page]==1)
	{
		#	echo 'stat_page='.$stat_page.'<hr>';#debug
		#
		$sth=db_query("SELECT * FROM g_stat_page WHERE stat_page_skin='$stat_skin' AND stat_page_page='$stat_page' AND stat_page_date='$stat_date'"); $op_type='INS';
		if($sth[1]>0) { $op_type='UPD'; $row=db_fetch($sth[0]); $stat_visit_visitors=$row[0][stat_page_visitors]; $stat_visit_unique=$row[0][stat_page_unique]; $stat_visit_reload=$row[0][stat_page_reload]; } else { $stat_visit_visitors=0; $stat_visit_unique=0; $stat_visit_reload=0; }
		#
		$args="SELECT * FROM g_stat_online WHERE stat_online_skin='$stat_skin' AND stat_online_date='$stat_date' AND stat_online_user='$stat_user' AND stat_online_page='$stat_page'";
		if(CheckUserIDOnline($args)=='unique') { $stat_visit_visitors++; $stat_visit_unique++; } else { $stat_visit_reload++; }
		#
		switch($op_type)
		{
			case 'INS':	$sth=db_query("INSERT INTO g_stat_page SET stat_page_skin='$stat_skin', stat_page_page='$stat_page', stat_page_date='$stat_date', stat_page_unique='$stat_visit_unique', stat_page_reload='$stat_visit_reload', stat_page_visitors='$stat_visit_visitors'");	break;
			case 'UPD':	$sth=db_query("UPDATE g_stat_page SET stat_page_unique='$stat_visit_unique', stat_page_reload='$stat_visit_reload', stat_page_visitors='$stat_visit_visitors' WHERE stat_page_skin='$stat_skin' AND stat_page_page='$stat_page' AND stat_page_date='$stat_date' LIMIT 1");	break;
		}
	}
	#
	#==================#
	#   Online users   #
	#==================#
	#
	if($_SESSION[misc][stat_user]==1)
	{
		#	echo 'stat_user='.$stat_user.'<hr>';#debug
		#
		$sth=db_query("SELECT * FROM g_stat_online WHERE stat_online_skin='$stat_skin' AND stat_online_date='$stat_date' AND stat_online_user='$stat_user' AND stat_online_page='$stat_page'");
		if($sth[1]==0)	{ $sth=db_query("INSERT INTO g_stat_online SET stat_online_skin='$stat_skin', stat_online_date='$stat_date', stat_online_user='$stat_user', stat_online_page='$stat_page', stat_online_time='$stat_timestamp', stat_online_ip='$IP'"); }
		else			{ $sth=db_query("UPDATE g_stat_online SET stat_online_time='$stat_timestamp' WHERE stat_online_skin='$stat_skin' AND stat_online_date='$stat_date' AND stat_online_user='$stat_user' AND stat_online_page='$stat_page' LIMIT 1"); }
		CheckUsersOnline();
	}
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#
#	common functions
#
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CheckUserIDOnline($args)
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
	$sth=db_query($args); if($sth[1]==0) { return('unique'); } else { return('reload'); }
}
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
function CheckUsersOnline()
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
	global $stat_skin,$stat_date,$stat_timestamp;
	#
	#	Remove online users not loading any page since n_mins
	#
	$stat_timestamp_del=$stat_timestamp-(350); # 5*60=350;
	$sth=db_query("DELETE FROM g_stat_online WHERE stat_online_skin='$stat_skin' AND stat_online_date<='$stat_date' AND stat_online_time<'$stat_timestamp_del'");
	#
	#	Count users online
	#
	$sth=db_query("SELECT DISTINCT stat_online_date, stat_online_user FROM g_stat_online WHERE stat_online_skin='$stat_skin' AND stat_online_date='$stat_date'");
	$now_online=$sth[1];
	#
	#	Update the max users online table if necessary
	#
	$max_online=0;
	$sth=db_query("SELECT * FROM g_stat_misc WHERE stat_misc_skin='$stat_skin' AND stat_misc_key='*max_online'");
	if($sth[1]>0)	{ $row=db_fetch($sth[0]); $max_online=$row[0][stat_misc_val]; }
	if($now_online>$max_online)
	{
		$now=date('YmdHi');
		if($sth[1]==0)	{ $sth=db_query("INSERT INTO g_stat_misc SET stat_misc_skin='$stat_skin', stat_misc_key='*max_online', stat_misc_val='$now_online', stat_misc_date='$now'"); } 
		else			{ $sth=db_query("UPDATE g_stat_misc SET stat_misc_val='$now_online', stat_misc_date='$now' WHERE stat_misc_skin='$stat_skin' AND stat_misc_key='*max_online'"); }
	}
	$_SESSION[u_online]=$now_online;
	$sth=db_query("SELECT SUM(stat_lang_visits) AS stat_lang_visits FROM g_stat_lang WHERE stat_lang_skin='$stat_skin'");
	$row=db_fetch($sth[0]);
	$_SESSION[t_online]=number_format($row[0][stat_lang_visits],0,'.','.');
	return;
}
?>
Return current item: phpCAMALEO