• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

php读取纯真ip数据库使用示例_php实例

php 搞代码 3年前 (2022-01-25) 14次浏览 已收录 0个评论
<?php<BR>/*--------------------------------------------------<BR> ip2address [qqwry.dat]<BR>--------------------------------------------------*/</P><P>class ip {<BR> var $fh; //IP数据库文件句柄<BR> var $first; //第一条索引<BR> var $last; //最后一条索引<BR> var $total; //索引总数</P><P> //构造函数<BR> function __construct() {<BR>  $this->fh = fopen('qqwry<strong style="color:transparent">¥本文来源gaodai#ma#com搞@@代~&码网^</strong><small>搞gaodaima代码</small>.dat', 'rb'); //qqwry.dat文件<BR>  $this->first = $this->getLong4();<BR>  $this->last = $this->getLong4();<BR>  $this->total = ($this->last - $this->first) / 7; //每条索引7字节<BR> }</P><P> //检查IP合法性<BR> function checkIp($ip) {<BR>  $arr = explode('.',$ip);<BR>  if(count($arr) !=4 ) {<BR>   return false;<BR>  } else {<BR>   for ($i=0; $i < 4; $i++) {<BR>    if ($arr[$i]  '255') {<BR>     return false;<BR>    }<BR>   }<BR>  }<BR>  return true;<BR> }</P><P> function getLong4() {<BR>  //读取little-endian编码的4个字节转化为长整型数<BR>  $result = unpack('Vlong', fread($this->fh, 4));<BR>  return $result['long'];<BR> }</P><P> function getLong3() {<BR>  //读取little-endian编码的3个字节转化为长整型数<BR>  $result = unpack('Vlong', fread($this->fh, 3).chr(0));<BR>  return $result['long'];<BR> }</P><P> //查询信息<BR> function getInfo($data = "") {<BR>  $char = fread($this->fh, 1);<BR>  while (ord($char) != 0) { //国家地区信息以0结束<BR>   $data .= $char;<BR>   $char = fread($this->fh, 1);<BR>  }<BR>  return $data;<BR> }</P><P> //查询地区信息<BR> function getArea() {<BR>  $byte = fread($this->fh, 1); //标志字节<BR>  switch (ord($byte)) {<BR>   case 0: $area = ''; break; //没有地区信息<BR>   case 1: //地区被重定向<BR>    fseek($this->fh, $this->getLong3());<BR>    $area = $this->getInfo(); break;<BR>   case 2: //地区被重定向<BR>   fseek($this->fh, $this->getLong3());<BR>   $area = $this->getInfo(); break;<BR>   default: $area = $this->getInfo($byte);  break; //地区没有被重定向<BR>  }<BR>  return $area;<BR> }</P><P> function ip2addr($ip) {<BR>  if(!$this -> checkIp($ip)){<BR>   return false;<BR>  }</P><P>  $ip = pack('N', intval(ip2long($ip)));</P><P>  //二分查找<BR>  $l = 0;<BR>  $r = $this->total;</P><P>  while($l <= $r) {<BR>   $m = floor(($l + $r) / 2); //计算中间索引<BR>   fseek($this->fh, $this->first + $m * 7);<BR>   $beginip = strrev(fread($this->fh, 4)); //中间索引的开始IP地址<BR>   fseek($this->fh, $this->getLong3());<BR>   $endip = strrev(fread($this->fh, 4)); //中间索引的结束IP地址</P><P>   if ($ip < $beginip) { //用户的IP小于中间索引的开始IP地址时<BR>    $r = $m - 1;<BR>   } else {<BR>    if ($ip > $endip) { //用户的IP大于中间索引的结束IP地址时<BR>     $l = $m + 1;<BR>    } else { //用户IP在中间索引的IP范围内时<BR>     $findip = $this->first + $m * 7;<BR>     break;<BR>    }<BR>   }<BR>  }</P><P>  //查询国家地区信息<BR>  fseek($this->fh, $findip);<BR>  $location['beginip'] = long2ip($this->getLong4()); //用户IP所在范围的开始地址<BR>  $offset = $this->getlong3();<BR>  fseek($this->fh, $offset);<BR>  $location['endip'] = long2ip($this->getLong4()); //用户IP所在范围的结束地址<BR>  $byte = fread($this->fh, 1); //标志字节<BR>  switch (ord($byte)) {<BR>   case 1:  //国家和区域信息都被重定向<BR>    $countryOffset = $this->getLong3(); //重定向地址<BR>    fseek($this->fh, $countryOffset);<BR>    $byte = fread($this->fh, 1); //标志字节<BR>    switch (ord($byte)) {<BR>     case 2: //国家信息被二次重定向<BR>      fseek($this->fh, $this->getLong3());<BR>      $location['country'] = $this->getInfo();<BR>      fseek($this->fh, $countryOffset + 4);<BR>      $location['area'] = $this->getArea();<BR>      break;<BR>     default: //国家信息没有被二次重定向<BR>      $location['country'] = $this->getInfo($byte);<BR>      $location['area'] = $this->getArea();<BR>      break;<BR>    }<BR>    break;</P><P>   case 2: //国家信息被重定向<BR>    fseek($this->fh, $this->getLong3());<BR>    $location['country'] = $this->getInfo();<BR>    fseek($this->fh, $offset + 8);<BR>    $location['area'] = $this->getArea();<BR>    break;</P><P>   default: //国家信息没有被重定向<BR>    $location['country'] = $this->getInfo($byte);<BR>    $location['area'] = $this->getArea();<BR>    break;<BR>  }</P><P>  //gb2312 to utf-8(去除无信息时显示的CZ88.NET)<BR>  foreach ($location as $k => $v) {<BR>   $location[$k] = str_replace('CZ88.NET','',iconv('gb2312', 'utf-8', $v));<BR>  }</P><P>  return $location;<BR> }</P><P> //析构函数<BR> function __destruct() {<BR>  fclose($this->fh);<BR> }<BR>}</P><P>$ip = new ip();<BR>$addr = $ip -> ip2addr('IP地址');<BR>print_r($addr);<BR>?><BR>

搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:php读取纯真ip数据库使用示例_php实例
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址