ip.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | Fanwe 方维直播系统
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2010 http://www.fanwe.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. class iplocate{
  8. var $fp;
  9. var $firstip; //第一条ip索引的偏移地址
  10. var $lastip; //最后一条ip索引的偏移地址
  11. var $totalip; //总ip数
  12. //*
  13. //构造函数,初始化一些变量
  14. //$datfile 的值为纯真IP数据库的名子,可自行修改.
  15. //*
  16. function iplocate(){
  17. $datfile = APP_ROOT_PATH."system/extend/ip.dat";
  18. $this->fp=fopen($datfile,'rb'); //二制方式打开
  19. $this->firstip = $this->get4b(); //第一条ip索引的绝对偏移地址
  20. $this->lastip = $this->get4b(); //最后一条ip索引的绝对偏移地址
  21. $this->totalip =($this->lastip - $this->firstip)/7 ; //ip总数 索引区是定长的7个字节,在此要除以7,
  22. register_shutdown_function(array($this,"closefp")); //为了兼容php5以下版本,本类没有用析构函数,自动关闭ip库.
  23. }
  24. //*
  25. //关闭ip库
  26. //*
  27. function closefp(){
  28. fclose($this->fp);
  29. }
  30. //*
  31. //读取4个字节并将解压成long的长模式
  32. //*
  33. function get4b(){
  34. $str=@unpack("V",fread($this->fp,4));
  35. return $str[1];
  36. }
  37. //*
  38. //读取重定向了的偏移地址
  39. //*
  40. function getoffset(){
  41. $str=@unpack("V",fread($this->fp,3).chr(0));
  42. return $str[1];
  43. }
  44. //*
  45. //读取ip的详细地址信息
  46. //*
  47. function getstr(){
  48. $split=fread($this->fp,1);
  49. while (ord($split)!=0) {
  50. $str .=$split;
  51. $split=fread($this->fp,1);
  52. }
  53. return $str;
  54. }
  55. //*
  56. //将ip通过ip2long转成ipv4的互联网地址,再将他压缩成big-endian字节序
  57. //用来和索引区内的ip地址做比较
  58. //*
  59. function iptoint($ip){
  60. return pack("N",intval(ip2long($ip)));
  61. }
  62. //*
  63. //获取客户端ip地址
  64. //注意:如果你想要把ip记录到服务器上,请在写库时先检查一下ip的数据是否安全.
  65. //*
  66. function getIP() {
  67. if (getenv('HTTP_CLIENT_IP')) {
  68. $ip = getenv('HTTP_CLIENT_IP');
  69. }
  70. elseif (getenv('HTTP_X_FORWARDED_FOR')) { //获取客户端用代理服务器访问时的真实ip 地址
  71. $ip = getenv('HTTP_X_FORWARDED_FOR');
  72. }
  73. elseif (getenv('HTTP_X_FORWARDED')) {
  74. $ip = getenv('HTTP_X_FORWARDED');
  75. }
  76. elseif (getenv('HTTP_FORWARDED_FOR')) {
  77. $ip = getenv('HTTP_FORWARDED_FOR');
  78. }
  79. elseif (getenv('HTTP_FORWARDED')) {
  80. $ip = getenv('HTTP_FORWARDED');
  81. }
  82. else {
  83. $ip = $_SERVER['REMOTE_ADDR'];
  84. }
  85. return $ip;
  86. }
  87. //*
  88. //获取地址信息
  89. //*
  90. function readaddress(){
  91. $now_offset=ftell($this->fp); //得到当前的指针位址
  92. $flag=$this->getflag();
  93. switch (ord($flag)){
  94. case 0:
  95. $address="";
  96. break;
  97. case 1:
  98. case 2:
  99. fseek($this->fp,$this->getoffset());
  100. $address=$this->getstr();
  101. break;
  102. default:
  103. fseek($this->fp,$now_offset);
  104. $address=$this->getstr();
  105. break;
  106. }
  107. return $address;
  108. }
  109. //*
  110. //获取标志1或2
  111. //用来确定地址是否重定向了.
  112. //*
  113. function getflag(){
  114. return fread($this->fp,1);
  115. }
  116. //*
  117. //用二分查找法在索引区内搜索ip
  118. //*
  119. function searchip($ip){
  120. $ip=gethostbyname($ip); //将域名转成ip
  121. $ip_offset["ip"]=$ip;
  122. $ip=$this->iptoint($ip); //将ip转换成长整型
  123. $firstip=0; //搜索的上边界
  124. $lastip=$this->totalip; //搜索的下边界
  125. $ipoffset=$this->lastip; //初始化为最后一条ip地址的偏移地址
  126. while ($firstip <= $lastip){
  127. $i=floor(($firstip + $lastip) / 2); //计算近似中间记录 floor函数记算给定浮点数小的最大整数,说白了就是四舍五也舍
  128. fseek($this->fp,$this->firstip + $i * 7); //定位指针到中间记录
  129. $startip=strrev(fread($this->fp,4)); //读取当前索引区内的开始ip地址,并将其little-endian的字节序转换成big-endian的字节序
  130. if ($ip < $startip) {
  131. $lastip=$i - 1;
  132. }
  133. else {
  134. fseek($this->fp,$this->getoffset());
  135. $endip=strrev(fread($this->fp,4));
  136. if ($ip > $endip){
  137. $firstip=$i + 1;
  138. }
  139. else {
  140. $ip_offset["offset"]=$this->firstip + $i * 7;
  141. break;
  142. }
  143. }
  144. }
  145. return $ip_offset;
  146. }
  147. //*
  148. //获取ip地址详细信息
  149. //*
  150. function getaddress($ip){
  151. $ip_offset=$this->searchip($ip); //获取ip 在索引区内的绝对编移地址
  152. $ipoffset=$ip_offset["offset"];
  153. $address["ip"]=$ip_offset["ip"];
  154. fseek($this->fp,$ipoffset); //定位到索引区
  155. $address["startip"]=long2ip($this->get4b()); //索引区内的开始ip 地址
  156. $address_offset=$this->getoffset(); //获取索引区内ip在ip记录区内的偏移地址
  157. fseek($this->fp,$address_offset); //定位到记录区内
  158. $address["endip"]=long2ip($this->get4b()); //记录区内的结束ip 地址
  159. $flag=$this->getflag(); //读取标志字节
  160. switch (ord($flag)) {
  161. case 1: //地区1地区2都重定向
  162. $address_offset=$this->getoffset(); //读取重定向地址
  163. fseek($this->fp,$address_offset); //定位指针到重定向的地址
  164. $flag=$this->getflag(); //读取标志字节
  165. switch (ord($flag)) {
  166. case 2: //地区1又一次重定向,
  167. fseek($this->fp,$this->getoffset());
  168. $address["area1"]=$this->getstr();
  169. fseek($this->fp,$address_offset+4); //跳4个字节
  170. $address["area2"]=$this->readaddress(); //地区2有可能重定向,有可能没有
  171. break;
  172. default: //地区1,地区2都没有重定向
  173. fseek($this->fp,$address_offset); //定位指针到重定向的地址
  174. $address["area1"]=$this->getstr();
  175. $address["area2"]=$this->readaddress();
  176. break;
  177. }
  178. break;
  179. case 2: //地区1重定向 地区2没有重定向
  180. $address1_offset=$this->getoffset(); //读取重定向地址
  181. fseek($this->fp,$address1_offset);
  182. $address["area1"]=$this->getstr();
  183. fseek($this->fp,$address_offset+8);
  184. $address["area2"]=$this->readaddress();
  185. break;
  186. default: //地区1地区2都没有重定向
  187. fseek($this->fp,$address_offset+4);
  188. $address["area1"]=$this->getstr();
  189. $address["area2"]=$this->readaddress();
  190. break;
  191. }
  192. //*过滤一些无用数据
  193. if (strpos($address["area1"],"CZ88.NET")!=false){
  194. $address["area1"]="未知";
  195. }
  196. if (strpos($address["area2"],"CZ88.NET")!=false){
  197. $address["area2"]=" ";
  198. }
  199. foreach($address as $k=>$item)
  200. {
  201. if(!$this->is_utf8($address[$k]))
  202. {
  203. $address[$k] = iconv('gbk','utf-8',$item);
  204. }
  205. }
  206. return $address;
  207. }
  208. function is_utf8($string)
  209. {
  210. return preg_match('%^(?:
  211. [\x09\x0A\x0D\x20-\x7E] # ASCII
  212. | [\xC2-\xDF][\x80-\xBF] # non-overlong 2-byte
  213. | \xE0[\xA0-\xBF][\x80-\xBF] # excluding overlongs
  214. | [\xE1-\xEC\xEE\xEF][\x80-\xBF]{2} # straight 3-byte
  215. | \xED[\x80-\x9F][\x80-\xBF] # excluding surrogates
  216. | \xF0[\x90-\xBF][\x80-\xBF]{2} # planes 1-3
  217. | [\xF1-\xF3][\x80-\xBF]{3} # planes 4-15
  218. | \xF4[\x80-\x8F][\x80-\xBF]{2} # plane 16
  219. )*$%xs', $string);
  220. }
  221. }
  222. ?>