소스 파일명 : stat_utils.php
001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
027
028
029
030
031
032
033
034
035
036
037
038
039
040
041
042
043
044
045
046
047
048
049
050
051
052
053
054
055
056
057
058
059
060
061
062
063
064
065
066
067
068
069
070
071
072
073
074
075
076
077
078
079
080
081
082
083
084
085
086
087
088
089
090

echo "<script type='text/javascript'>
			$( function() {
				$(document).on('click', '.onedrive',
					function() {
						window.location.href ='./".basename($_SERVER['SCRIPT_FILENAME'])."?parm=onedrive' ;
					});
			});
		</script>";

function html_access ($script_filename,$dbi,$no='',$sh='') {

		if (strlen($script_filename)>30) exit;
		$script_filename = mysqli_real_escape_string($dbi,$script_filename);

		$remote_addr = $_SERVER['REMOTE_ADDR'];
		// user_agent
		$u_a = $_SERVER['HTTP_USER_AGENT'];
		$m_referer= substr($_SERVER['HTTP_REFERER'],0,254);
		$m_referer= mysqli_real_escape_string($dbi,trim($m_referer));
		// query_string
		$m_query_str = substr($_SERVER['QUERY_STRING'],0,255);

	if ($remote_addr == '192.168.0.1' or isset($_COOKIE['login_name'])) return;

	if ( ! is_not_robot() ) return;

	if ( ! empty($no) and $script_filename == 'view.php' ) {

		mysqli_query($dbi,"insert into dict.ip_count (ip,last_login,access_cnt,day_access_cnt,first_login) values ('{$remote_addr}',now(),1,1,now()) on duplicate key update last_login=now(),access_cnt=(access_cnt+1),day_access_cnt=if(dayofyear(now())=dayofyear(last_login),day_access_cnt+1,1) "); 
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

		mysqli_query($dbi,"insert into dict.dict_count (no,ip,search_date,no_match,robot,u_a,referer,query_str) values ('$no','{$remote_addr}',now(),'$sh','$robot','{$u_a}','{$m_referer}','{$m_query_str}')");
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

	}

	$result = mysqli_query($dbi,"select * from dict.ip_count where ip='{$remote_addr}' ");
		if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	if( mysqli_num_rows($result) == 0 ) return;

	$result_chk = mysqli_query($dbi,"select * from dict.stat_page where page_name='$script_filename' and stat_date=date_format(now(),'%Y-%m-%d') and last_ip='{$remote_addr}' "); // 
		if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	$row_chk = mysqli_fetch_assoc($result_chk);

	if ( mysqli_num_rows($result_chk) == 0 ) {
		mysqli_query($dbi,"insert into dict.stat_page (page_name,stat_date,last_ip,last_access,access_num,user_agent) values ('$script_filename', now(), '{$remote_addr}', now(), 1, '{$u_a}' )"); 
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	}

	if ( mysqli_num_rows($result_chk) > 0 ) {
		$query = "update dict.stat_page set access_num=access_num+1, last_access=now() where page_name='$script_filename' and stat_date = '".date("Y-m-d")."' and last_ip='{$remote_addr}' ";
		mysqli_query($dbi,$query); 
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	}

}

function is_not_robot() {

	$robot_arr = array('Googlebot','Bot','cowbot','empas','Yahoo! Slurp','WISEbot','BizBot04','CaliforniaBrownSpider','msnbot','1Noonbot','bingbot','Daumoa','Yeti/','MJ12bot','Google Web Preview','HTTrack','crawler','http_load 29jun2005','TurnitinBot','FunWebProducts','SiteBot/','QrobotW','ezooms.bot@','Microsoft URL Control','KISA','discobot','robot','pirst','spider/','lycosa.','checkprivacy','Crawler','Preview','bsalsa.com','nutch agent','NAVER','360Spider','.org_bot','WebCapture ','instapaper','Ezooms',' DigExt','siteexplorer','Nutch','flipboard','wotbox','Navigator','linkAnalyze','Korea Internet & Security Agency','_bot');
	foreach($robot_arr as $key => $value) {
		if(strpos($_SERVER['HTTP_USER_AGENT'], $value)!==false) {
			return false; // this is robot !!!
		}
	}
	return true;
}

?>