<?php header("Content-type: text/html; charset=utf-8"); include 'CsvReader.php'; include 'conn.php'; include 'BinarySearch.php'; $prov = array("北京", "天津", "上海", "重庆", "河北", "河南", "云南", "辽宁", "黑龙江", "湖南", "安徽", "山东", "新疆", "江苏", "浙江", "江西", "湖北", "广西", "甘肃", "山西", "内蒙古", "陕西", "吉林", "福建", "贵州", "广东", "青海", "西藏", "四川", "宁夏", "海南", "台湾", "香港", "澳门"); $flow = array(); // $sql = "select * from bxmap.lib"; // $result = mysql_query($sql); $weblog_file = 'traffic_web.csv'; $weblogreader = new CsvReader($weblog_file); $line_number = $weblogreader->get_lines(); echo "Number of all records is " . $line_number . "<br>"; $data = $weblogreader->get_data(1000000, 13000000); $index = 0; foreach ($data as $each) { $ip = ip2long($each[2]); $time = strtotime($each[1]); $hour = (int) date('H', $time); // foreach ($iplist as $eachip) { // if($eachip[1]>=$ip) { // $ip_prov=$eachip[5]; // break; // } // } // $sql = "select * from bxmap.lib where ipEnd_int >= '$ip' limit 1"; // $result = mysql_query($sql); // $result_row = mysql_fetch_array($result); $ip_prov = BinarySearch($ip); if ($ip_prov) {
<?php $lib_file = 'ip_lib.csv'; $libreader = new CsvReader($lib_file); $ip_line_number = $libreader->get_lines(); $iplist = $libreader->get_data($ip_line_number); //echo $ip_line_number, chr(10); //echo "<br>"; function BinarySearch($search_ip) { global $ip_line_number; global $iplist; $low = 0; $high = $ip_line_number - 1; while ($low < $high) { $current = intval(($low + $high) / 2); if ($search_ip >= $iplist[$current][0] && $search_ip <= $iplist[$current][1]) { return $iplist[$current][5]; } if ($search_ip > $iplist[$current][1]) { $low = $current; } else { $high = $current; } } // echo $search_ip."<br>"; return 0; }