= 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);
  • 太原网友:谁来管管逢雨必淹的山大附小周边 2019-05-19
  • 轻松打造浅色复古家居 2019-05-17
  • 乌鲁木齐确定今年实施“十大民生实事” 2019-05-17
  • 贸易战升级!七成加拿大民众考虑不再购买美国货 2019-05-17
  • 【大家谈】让实干成为党员干部的标配 2019-05-10
  • 一个连基本语句都不懂的人只会瞎扯,看着就想笑 2019-05-02
  • 沪新增医疗服务最高价标准 水中分娩限2500元次 2019-05-01
  • 2022年冬奥会筹备进行时 2019-04-30
  • 五月呼声报告: 陕11家单位上黑榜 官方回复房产投诉满意度低 2019-04-30
  • 河南一矿企收33张罚单仍违法排污 有基层官员站台 2019-04-29
  • 青锋的专栏作者中国国家地理网 2019-04-23
  • 走奋发图强之路,壮我中华科技实力。 2019-04-20
  • 楚天传媒大厦招租信息 2019-04-19
  • 高清:12处唐长安城遗存照,纪念大唐建都长安1400周年 2019-04-19
  • 出租广州市番禺区大龙街东兴路476号4楼物业空置部分 2019-04-18