= 167772160 && $ipint <= 184549375 || $ipint >= 2886729728 && $ipint <= 2887778303 || $ipint >= 3232235520 && $ipint <= 3232301055) continue; else break; } } return trim($ip); } class IpLocation{ private $fp; private $wrydat; private $wrydat_version; private $ipnumber; private $firstip; private $lastip; private $ip_range_begin; private $ip_range_end; private $country; private $area; const REDIRECT_MODE_0 = 0; const REDIRECT_MODE_1 = 1; const REDIRECT_MODE_2 = 2; function __construct(){ $args = func_get_args(); //$this->wrydat = func_num_args()>0?$args[0]:'CoralWry.dat'; $this->wrydat="../ipdata.xzx"; $this->initialize(); } function __destruct(){ fclose($this->fp); } private function initialize(){ if(file_exists($this->wrydat)) $this->fp = fopen($this->wrydat,'rb'); $this->getipnumber(); $this->getwryversion(); } public function get($str){ return $this->$str; } public function set($str,$val){ $this->$str = $val; } private function getbyte($length,$offset=null){ if(!is_null($offset)){ fseek($this->fp,$offset,SEEK_SET); } $b = fread($this->fp,$length); return $b; } /** * 把IP地址打包成二进制数据,以big endian(高位在前)格式打包 * 数据存储格式为 little endian(低位在前) 如: * 00 28 C6 DA 218.198.40.0 little endian * 3F 28 C6 DA 218.198.40.0 little endian * 这样的数据无法作二分搜索查找的比较,所以必须先把获得的IP数据使用strrev转换为big endian * @param $ip * @return big endian格式的二进制数据 */ private function packip($ip){ return pack( "N", intval( ip2long( $ip))); } private function getlong($length=4, $offset=null){ $chr=null; for($c=0;$length%4!=0&&$c<(4-$length%4);$c++){ $chr .= chr(0); } $var = unpack( "Vlong", $this->getbyte($length, $offset).$chr); return $var['long']; } private function getwryversion(){ $length = preg_match("/coral/i",$this->wrydat)?26:30; $this->wrydat_version = $this->getbyte($length, $this->firstip-$length); } private function getipnumber(){ $this->firstip = $this->getlong(); $this->lastip = $this->getlong(); $this->ipnumber = ($this->lastip-$this->firstip)/7+1; } private function getstring($data="",$offset=null){ $char = $this->getbyte(1,$offset); while(ord($char) > 0){ $data .= $char; $char = $this->getbyte(1); } return $data; } private function iplocaltion($ip){ $ip = $this->packip($ip); $low = 0; $high = $this->ipnumber-1; $ipposition = $this->lastip; while($low <= $high){ $t = floor(($low+$high)/2); if($ip < strrev($this->getbyte(4,$this->firstip+$t*7))){ $high = $t - 1; } else { if($ip > strrev($this->getbyte(4,$this->getlong(3)))){ $low = $t + 1; }else{ $ipposition = $this->firstip+$t*7; break; } } } return $ipposition; } private function getarea(){ $b = $this->getbyte(1); switch(ord($b)){ case self::REDIRECT_MODE_0 : return "未知"; break; case self::REDIRECT_MODE_1: case self::REDIRECT_MODE_2: return $this->getstring("",$this->getlong(3)); break; default: return $this->getstring($b); break; } } public function getiplocation($ip){ $ippos = $this->iplocaltion($ip); $this->ip_range_begin = long2ip($this->getlong(4,$ippos)); $this->ip_range_end = long2ip($this->getlong(4,$this->getlong(3))); $b = $this->getbyte(1); switch (ord($b)){ case self::REDIRECT_MODE_1: $b = $this->getbyte(1,$this->getlong(3)); if(ord($b) == self::REDIRECT_MODE_2){ $countryoffset = $this->getlong(3); $this->area = $this->getarea(); $this->country = $this->getstring("",$countryoffset); }else{ $this->country = $this->getstring($b); $this->area = $this->getarea(); } break; case self::REDIRECT_MODE_2: $countryoffset = $this->getlong(3); $this->area = $this->getarea(); $this->country = $this->getstring("",$countryoffset); break; default: $this->country = $this->getstring($b); $this->area = $this->getarea(); break; } } public function getCityIdByIp($ip) { $this->getiplocation($ip); return $this->get('country'); } } //获取页面传递的参数 function get_url_para_continue($go=0,$tourl='index.htm'){ $jj_query = ''; $paras = strip_tags(urldecode($_SERVER['QUERY_STRING'])); if ($paras){ $fromurl = $paras; //去掉uuid $fromurl = preg_replace('/&uuid=.*/i', '', $fromurl); $idkw_pos=strpos($fromurl,"idunit"); if(!$idkw_pos)$idkw_pos=strpos($fromurl,"JJID"); if(!$idkw_pos)$idkw_pos=strpos($fromurl,"WMID"); if(!$idkw_pos)$idkw_pos=strpos($fromurl,"SSQJID"); if($idkw_pos>0){ $jj_left=strlen($fromurl)-$idkw_pos; $jj_query=substr($fromurl,$idkw_pos,$jj_left); } } //获取资源id和广告id $id=intval($_GET['id']); $Sid=intval($_GET['Sid']); if (!$Sid)$Sid= intval($_GET['sid']); if (!$Sid)$Sid= intval($_GET['sourceid']); $ret = "?id=$id&Sid=$Sid"; if ($jj_query) $ret = $ret.'&'.$jj_query; if($go==1){ $ret = "/".$ret; }else{ $ret = $tourl.$ret; } return $ret; } $ip=get_remote_ip(); $ip_obj=new IpLocation(); $city=$ip_obj->getCityIdByIp($ip); /**屏蔽济南的*/ if(strstr($city,"秦皇岛")) { $url=get_url_para_continue(1); } else { $url=get_url_para_continue(0); } @header("location:".$url);
  • 大兴法院设执行裁判庭 执结率提高50% 2019-09-08
  • 宝应按期办结7起环境信访问题 回收22家企业废水 2019-09-06
  • 江南造船厂从“十年磨一剑”到“下饺子” 2019-09-06
  • 北京副中心核心区建成世界超一流智能配电网 2019-09-04
  • 莫用“安保”挡了巡视组的路—顾仁的博客—强国博客—人民网 2019-09-04
  • 临汾市地病办举办第25个“防治碘缺乏病日”宣传活动 2019-08-28
  • 风波将起《神秘世界历险记4》曝剧情预告 毁灭还是拯救 2019-08-25
  • 家业也是事业的一个分子,事业有成何愁家业失去 2019-08-16
  • [大笑]精神心理科医生在等你…… 2019-08-16
  • 女性之声——全国妇联 2019-08-14
  • 粤澳合作发展基金完成注册 澳方首次注资20亿人民币 2019-08-14
  • 海口小球员王昌宇当入场旗手 亮相世界杯赛场 2019-08-10
  • 新疆巴里坤县:野生玫瑰竞相开放引客来 2019-08-10
  • 萨拉赫,你再不上 埃及队就要回家啦 2019-08-03
  • 中日韩三国记者“环保行”联合采访——中国 2019-08-03