IpLocation.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. <?php
  2. namespace App;
  3. class IpLocation {
  4. var $fp;
  5. var $firstip;//第一条ip索引的偏移地址
  6. var $lastip; //最后一条ip索引的偏移地址
  7. var $totalip;//总ip数
  8. public static $ipl = null;
  9. public static function getIPAddress() {
  10. if (!IpLocation::$ipl) IpLocation::$ipl = new IpLocation();
  11. $ip = IpLocation::$ipl->getIP();
  12. if (!$ip) return false;
  13. $address = IpLocation::$ipl->getaddress($ip);
  14. return mb_convert_encoding( $address ["area1"] . $address ["area2"], 'utf-8', 'GB2312' );
  15. }
  16. public static function getIPAddress2() {
  17. if (!IpLocation::$ipl) IpLocation::$ipl = new IpLocation();
  18. $ip = IpLocation::$ipl->getIP();
  19. if (!$ip) return false;
  20. return IpLocation::$ipl->getaddress($ip);
  21. }
  22. public static function getAddressByIP($ip) {
  23. if (!$ip) return "未知";
  24. if (!IpLocation::$ipl) IpLocation::$ipl = new IpLocation();
  25. $address = IpLocation::$ipl->getaddress($ip);
  26. return mb_convert_encoding( $address ["area1"] . $address ["area2"], 'utf-8', 'GB2312' );
  27. }
  28. public function getAddressByIP_new($ip = ''){
  29. $ch = curl_init();
  30. $url = 'https://whois.pconline.com.cn/ipJson.jsp?ip=' . $ip;
  31. curl_setopt($ch, CURLOPT_URL, $url);
  32. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  33. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  34. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  35. $data = curl_exec($ch);
  36. curl_close($ch);
  37. $data = mb_convert_encoding($data, 'utf-8', 'GB2312'); // 转换编码
  38. // 截取{}中的字符串
  39. $data = substr($data, strlen('({') + strpos($data, '({'), (strlen($data) - strpos($data, '})')) * (-1));
  40. // 将截取的字符串$data中的‘,’替换成‘&’ 将字符串中的‘:‘替换成‘=’
  41. $data = str_replace('"', "", str_replace(":", "=", str_replace(",", "&", $data)));
  42. parse_str($data, $addressInfo); // 将字符串转换成数组格式
  43. return $addressInfo['addr']; // 返回ip归属地
  44. }
  45. public function __construct(){
  46. $datfile = storage_path('').'/qqwry-2021.dat';
  47. $this->fp=fopen($datfile,'rb')or die("QQWry.Dat不存在,请去网上下載纯真IP数据库, 'QQWry.dat' 放到当前目录下"); //二制方式打开
  48. $this->firstip = $this->get4b(); //第一条ip索引的绝对偏移地址
  49. $this->lastip = $this->get4b();//最后一条ip索引的绝对偏移地址
  50. $this->totalip =($this->lastip - $this->firstip)/7 ; //ip總數 索引区是定长的7个字节,在此要除以7,
  51. register_shutdown_function(array($this,"closefp"));//为了兼容php5以下版本,本类没有用析构函数,自动关闭ip库.
  52. }
  53. function closefp(){
  54. fclose($this->fp);
  55. }
  56. function get4b(){
  57. $str=unpack("V",fread($this->fp,4));
  58. return $str[1];
  59. }
  60. function getoffset(){
  61. $str=unpack("V",fread($this->fp,3).chr(0));
  62. return $str[1];
  63. }
  64. function getstr(){
  65. $str = '';
  66. $split=fread($this->fp,1);
  67. while (ord($split)!=0) {
  68. $str .=$split;
  69. $split=fread($this->fp,1);
  70. }
  71. return $str;
  72. }
  73. function iptoint($ip){
  74. return pack("N",intval(ip2long($ip)));
  75. }
  76. public static function getRealIp()
  77. {
  78. $headers = [
  79. 'HTTP_X_REAL_IP',
  80. 'HTTP_CLIENT_IP',
  81. 'HTTP_X_FORWARDED_FOR',
  82. 'HTTP_X_FORWARDED',
  83. 'HTTP_FORWARDED_FOR',
  84. 'HTTP_FORWARDED',
  85. 'REMOTE_ADDR'
  86. ];
  87. foreach ($headers as $header) {
  88. if (!empty($_SERVER[$header])) {
  89. $ip = $_SERVER[$header];
  90. // 处理可能存在的多个 IP 地址的情况(例如,逗号分隔的 IP 列表)
  91. if (strpos($ip, ',') !== false) {
  92. $ipArray = explode(',', $ip);
  93. foreach ($ipArray as $individualIp) {
  94. // 去除多余的空格,并检查是否为有效 IP 地址
  95. $trimmedIp = trim($individualIp);
  96. if (filter_var($trimmedIp, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  97. return $trimmedIp;
  98. }
  99. }
  100. } else {
  101. // 如果只有一个 IP 地址,直接验证并返回
  102. if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE)) {
  103. return $ip;
  104. }
  105. }
  106. }
  107. }
  108. return 'UNKNOWN';
  109. }
  110. public static function getIP() {
  111. $onlineip = '';
  112. if (getenv('HTTP_CLIENT_IP')&& strcasecmp(getenv('HTTP_CLIENT_IP' ), 'unknown' )) {
  113. $onlineip = getenv('HTTP_CLIENT_IP' );
  114. } elseif (getenv('HTTP_X_FORWARDED_FOR')&& strcasecmp(getenv('HTTP_X_FORWARDED_FOR' ), 'unknown' )) {
  115. $onlineip = getenv('HTTP_X_FORWARDED_FOR' );
  116. } elseif (getenv('REMOTE_ADDR')&& strcasecmp(getenv('REMOTE_ADDR' ), 'unknown' )) {
  117. $onlineip = getenv('REMOTE_ADDR' );
  118. } elseif (isset($_SERVER ['REMOTE_ADDR'])&& $_SERVER ['REMOTE_ADDR'] && strcasecmp($_SERVER ['REMOTE_ADDR'], 'unknown' )) {
  119. $onlineip = $_SERVER ['REMOTE_ADDR'];
  120. }
  121. preg_match("/[\d\.]{7,15}/", $onlineip, $onlineipmatches);
  122. return isset($onlineipmatches[0]) ? $onlineipmatches [0] : false;
  123. }
  124. function readaddress(){
  125. $now_offset=ftell($this->fp); //得到当前的指针位址
  126. $flag=$this->getflag();
  127. switch (ord($flag)){
  128. case 0:
  129. $address="";
  130. break;
  131. case 1:
  132. case 2:
  133. fseek($this->fp,$this->getoffset());
  134. $address=$this->getstr();
  135. break;
  136. default:
  137. fseek($this->fp,$now_offset);
  138. $address=$this->getstr();
  139. break;
  140. }
  141. return $address;
  142. }
  143. function getflag(){
  144. return fread($this->fp,1);
  145. }
  146. function searchip($ip){
  147. $ip=gethostbyname($ip); //将域名转成ip
  148. $ip_offset["ip"]=$ip;
  149. $ip=$this->iptoint($ip);//将ip转换成长整型
  150. $firstip=0; //搜索的上边界
  151. $lastip=$this->totalip; //搜索的下边界
  152. $ipoffset=$this->lastip;//初始化为最后一条ip地址的偏移地址
  153. while ($firstip <= $lastip){
  154. $i=floor(($firstip + $lastip) / 2);//计算近似中间记录 floor函数记算给定浮点数小的最大整数,说白了就是四舍五也舍
  155. fseek($this->fp,$this->firstip + $i * 7);//定位指针到中间记录
  156. $startip=strrev(fread($this->fp,4)); //读取当前索引区内的开始ip地址,并将其little-endian的字节序转换成big-endian的字节序
  157. if ($ip < $startip) {
  158. $lastip=$i - 1;
  159. } else {
  160. fseek($this->fp,$this->getoffset());
  161. $endip=strrev(fread($this->fp,4));
  162. if ($ip > $endip){
  163. $firstip=$i + 1;
  164. } else {
  165. $ip_offset["offset"]=$this->firstip + $i * 7;
  166. break;
  167. }
  168. }
  169. }
  170. return $ip_offset;
  171. }
  172. function getaddress($ip){
  173. $ip_offset=$this->searchip($ip);//获取ip 在索引区内的绝对编移地址
  174. $ipoffset=$ip_offset["offset"];
  175. $address["ip"]=$ip_offset["ip"];
  176. fseek($this->fp,$ipoffset);//定位到索引区
  177. $address["startip"]=long2ip($this->get4b()); //索引区内的开始ip 地址
  178. $address_offset=$this->getoffset();//获取索引区内ip在ip记录区内的偏移地址
  179. fseek($this->fp,$address_offset);//定位到记录区内
  180. $address["endip"]=long2ip($this->get4b()); //记录区内的结束ip 地址
  181. $flag=$this->getflag();//读取标志字节
  182. switch (ord($flag)) {
  183. case 1://地区1地区2都重定向
  184. $address_offset=$this->getoffset(); //读取重定向地址
  185. fseek($this->fp,$address_offset); //定位指针到重定向的地址
  186. $flag=$this->getflag(); //读取标志字节
  187. switch (ord($flag)) {
  188. case 2://地区1又一次重定向,
  189. fseek($this->fp,$this->getoffset());
  190. $address["area1"]=$this->getstr();
  191. fseek($this->fp,$address_offset+4);//跳4个字节
  192. $address["area2"]=$this->readaddress();//地区2有可能重定向,有可能没有
  193. break;
  194. default: //地区1,地区2都没有重定向
  195. fseek($this->fp,$address_offset);//定位指针到重定向的地址
  196. $address["area1"]=$this->getstr();
  197. $address["area2"]=$this->readaddress();
  198. break;
  199. }
  200. break;
  201. case 2: //地区1重定向 地区2没有重定向
  202. $address1_offset=$this->getoffset(); //读取重定向地址
  203. fseek($this->fp,$address1_offset);
  204. $address["area1"]=$this->getstr();
  205. fseek($this->fp,$address_offset+8);
  206. $address["area2"]=$this->readaddress();
  207. break;
  208. default: //地区1地区2都没有重定向
  209. fseek($this->fp,$address_offset+4);
  210. $address["area1"]=$this->getstr();
  211. $address["area2"]=$this->readaddress();
  212. break;
  213. }
  214. //*过滤一些无用数据
  215. if (strpos($address["area1"],"CZ88.NET")!=false){
  216. $address["area1"]="未知";
  217. }
  218. if (strpos($address["area2"],"CZ88.NET")!=false){
  219. $address["area2"]=" ";
  220. }
  221. return $address;
  222. }
  223. }