소스 파일명 : stat_function.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
091
092
093
094
095
096
097
098
099
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
<?php
### (2017.9.2, 차재복, Cha Jae Bok, cjbword@gmailcom)
?>

<?php // 통계 관련 사용자 정의 함수들

// 접속 디테일 보여주기
function stat_display ($m_edit_level,$dbi) {

		$query = "select * from dict.stat_page where page_name='knowhow.php' and date_format(last_access,'%Y-%m-%d')=date_format(now(),'%Y-%m-%d') order by last_access desc";
		$result=mysqli_query($dbi,$query);
			if (mysqli_errno($dbi)) { echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
		echo "오늘 접속자 수 : ".mysqli_num_rows($result)."<br>";		
		if ($m_edit_level == 'super') {
			$i = 1;
			while ($matched=mysqli_fetch_assoc($result)) {
				echo "&nbsp;&nbsp;&nbsp;"." $i) 접속 ".$matched[access_num]."회 ; ";
				echo "ip <a href=http://whois.kisa.or.kr/openapi/whois.jsp?query=$matched[last_ip]&key=2013031411240702592637>".$matched[last_ip]."</a> ; ";
				echo "time ".date_format(date_create($matched[last_access]),'H:i')."<br>";
				$i = $i + 1;
			}
		} 

}

// 웹페이지별 접근 기록 ******************************************************************
function html_access ($script_filename,$m_edit_level,$dbi) {

	// local 또는 editor 이면 기록 취소
	if ($_SERVER['REMOTE_ADDR'] == '192.168.0.1' or $_SERVER['REMOTE_ADDR'] == '127.0.0.1' or $m_edit_level == 'super') return;

/*
	// 용어 해설 보지 않고 처음 방문한 경우 리턴
	$ip_chk = mysqli_query($dbi,"select * from ip_count where ip='".$_SERVER['REMOTE_ADDR']."'");  
		if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	if ( mysqli_num_rows($ip_chk) < 1 ) return;
*/

//	echo $_SERVER[REMOTE_ADDR]."<br>";
//	echo inet_pton($_SERVER[REMOTE_ADDR]);

	// 동일한 웹페이지/날짜/ip/user_agent 유무
/*
	$result_chk = mysqli_query($dbi,"select * from stat_page 
								where page_name='$script_filename' 
									and stat_date=date_format(now(),'%Y-%m-%d') 
									and ( last_ip='".$_SERVER[REMOTE_ADDR]."' 
										or inet_ip=inet'".inet_pton($_SERVER[REMOTE_ADDR])."' ) 
									and user_agent='".$_SERVER[HTTP_USER_AGENT]."'"); 
*/
	$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='".$_SERVER[REMOTE_ADDR]."' 
									and user_agent='".$_SERVER[HTTP_USER_AGENT]."'"); 
		if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	$row_chk = mysqli_fetch_assoc($result_chk);

	// robot 여부 체크
	$robot='';
	$is_not_robot = is_not_robot($robot);
	if ($is_not_robot === false) return;
	
	// insert
	if ( mysqli_num_rows($result_chk) == 0 and $is_not_robot  ) {  //  
/*
		mysqli_query($dbi,"insert into stat_page (page_name,stat_date,last_ip,inet_ip,last_access,access_num,user_agent) values ('$script_filename', now(), '".$_SERVER['REMOTE_ADDR']."','".inet_pton($_SERVER['REMOTE_ADDR'])."', now(), 1 , '".$_SERVER['HTTP_USER_AGENT']."')"); 
*/
		mysqli_query($dbi,"insert into dict.stat_page (page_name,stat_date,last_ip,last_access,access_num,user_agent) values ('$script_filename', now(), '".$_SERVER['REMOTE_ADDR']."', now(), 1 , '".$_SERVER['HTTP_USER_AGENT']."')"); 
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}		
	}

	// update
	if ( mysqli_num_rows($result_chk) > 0 and $is_not_robot ) {  //  
/*
		$query = "update 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='".$_SERVER['REMOTE_ADDR']."' or inet_ip='".inet_pton($_SERVER['REMOTE_ADDR'])."' ) and user_agent='".$_SERVER['HTTP_USER_AGENT']."' ";
*/
		$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='".$_SERVER['REMOTE_ADDR']."' and user_agent='".$_SERVER['HTTP_USER_AGENT']."' ";
		mysqli_query($dbi,$query); 
			if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}
	}

}

// robot 여부를 확인 *********************************************************
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) {
		// robot 이면,
		if(strpos($_SERVER['HTTP_USER_AGENT'],$value)!==false) {
			return false; // this is robot
		}
	}
	return true;
}


// ip address to ip number (출처 : https://inkplant.com/code/ipv6-to-number) ************************
// 이를 이용하여 ip 주소가 어떤 범위에 포함되는지 아닌지 결정 가능 (IPv4 및 IPv6 모두 가능)
function ipaddress_to_ipnumber($ipaddress) {
	$pton = @inet_pton($ipaddress);
	if (!$pton) { return false; }
	$number = '';
	foreach (unpack('C*', $pton) as $byte) {
		$number .= str_pad(decbin($byte), 8, '0', STR_PAD_LEFT);
	}
	return base_convert(ltrim($number, '0'), 2, 10);
}


function access_log ($id,$m_edit_level,$dbi) {

	// 서버 변수
	$remote_addr = $_SERVER['REMOTE_ADDR'];
	$u_a = substr($_SERVER['HTTP_USER_AGENT'],0,255); // user_agent 스트링 길이 제한
	$referer = substr($_SERVER['HTTP_REFERER'],0,255); // referer 스트링 길이 제한
	$query_str = substr($_SERVER['QUERY_STRING'],0,255); // url 쿼리 스트링 길이 제한

	// 통계 대상 걸러내기
	if (empty($id)) return;				// 빈 id 여부
	if (! is_not_robot()) return;		// robot 여부
	if ( $remote_addr == '192.168.0.1' or $remote_addr == '127.0.0.1' or $m_edit_level == 'super') return;		// localhost 등 여부

	// 각 id에 접속하면, record insert
	$qry="insert into access_count (id,ip,search_date,u_a,referer,query_str) values ($id,'$remote_addr',now(),'$u_a','$referer','$query_str')";
	mysqli_query($dbi,$qry); 
		if (mysqli_errno($dbi)) {echo mysqli_errno($dbi)." : ".mysqli_error($dbi)."\n";}

}

?>