国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

PHP判斷IP并跳轉到相應城市分站的方法

2024-05-04 23:33:22
字體:
來源:轉載
供稿:網友

這篇文章主要介紹了PHP判斷IP并轉跳到相應城市分站的方法,實例分析了php解析URL及跳轉的技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實例講述了PHP判斷IP并轉跳到相應城市分站的方法。分享給大家供大家參考。具體實現方法如下:

 

 
  1. <?php  
  2. class QQWry{  
  3. var $StartIP=0;  
  4. var $EndIP=0;  
  5. var $Country='';  
  6. var $Local='';  
  7. var $CountryFlag=0; 
  8. // 標識 Country位置  
  9. // 0x01,隨后3字節為Country偏移,沒有Local  
  10. // 0x02,隨后3字節為Country偏移,接著是Local  
  11. // 其他,Country,Local,Local有類似的壓縮。可能多重引用。  
  12. var $fp;  
  13. var $FirstStartIp=0;  
  14. var $LastStartIp=0;  
  15. var $EndIpOff=0 ;  
  16. function getStartIp($RecNo){  
  17. $offset=$this->FirstStartIp+$RecNo * 7 ;  
  18. @fseek($this->fp,$offset,SEEK_SET) ;  
  19. $buf=fread($this->fp ,7) ;  
  20. $this->EndIpOff=ord($buf[4])+(ord($buf[5])*256)+(ord($buf[6])*256*256); 
  21. $this->StartIp=ord($buf[0])+(ord($buf[1])*256)+(ord($buf[2])*256*256)+(ord($buf[3])*256*256*256); 
  22. return $this->StartIp; 
  23. function getEndIp(){  
  24. @fseek ( $this->fp , $this->EndIpOff , SEEK_SET ) ;  
  25. $buf=fread ( $this->fp , 5 ) ;  
  26. $this->EndIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256); 
  27. $this->CountryFlag=ord ( $buf[4] ) ;  
  28. return $this->EndIp ;  
  29. }  
  30. function getCountry(){  
  31. switch ( $this->CountryFlag ) {  
  32. case 1:  
  33. case 2:  
  34. $this->Country=$this->getFlagStr ( $this->EndIpOff+4) ;  
  35. //echo sprintf('EndIpOffset=(%x)',$this->EndIpOff );  
  36. $this->Local=( 1 == $this->CountryFlag )? '' : $this->getFlagStr ( $this->EndIpOff+8); 
  37. break ;  
  38. default :  
  39. $this->Country=$this->getFlagStr ($this->EndIpOff+4);  
  40. $this->Local=$this->getFlagStr ( ftell ( $this->fp ));  
  41. }  
  42. }  
  43. function getFlagStr ($offset){  
  44. $flag=0 ;  
  45. while(1){  
  46. @fseek($this->fp ,$offset,SEEK_SET) ;  
  47. $flag=ord(fgetc($this->fp ) ) ;  
  48. if ( $flag == 1 || $flag == 2 ) {  
  49. $buf=fread ($this->fp , 3 ) ;  
  50. if ($flag==2){  
  51. $this->CountryFlag=2;  
  52. $this->EndIpOff=$offset - 4 ;  
  53. }  
  54. $offset=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])* 256*256);  
  55. }  
  56. else{  
  57. break ;  
  58. }  
  59. }  
  60. if($offset<12)  
  61. return '';  
  62. @fseek($this->fp , $offset , SEEK_SET ) ;  
  63.  
  64. return $this->getStr();  
  65. }  
  66. function getStr ( )  
  67. {  
  68. $str='' ;  
  69. while ( 1 ) {  
  70. $c=fgetc ( $this->fp ) ;  
  71. //echo "$cn" ;  
  72. if(ord($c[0])== 0 )  
  73. break ;  
  74. $str.= $c ;  
  75. }  
  76. //echo "$str n";  
  77. return $str ;  
  78. }  
  79. function qqwry ($dotip='') {  
  80. if( !is_string($dotip) || $dotip==''){return;}  
  81. if(preg_match("/^127/",$dotip)){$this->Country="本地網絡";return ;}  
  82. elseif(preg_match("/^192/",$dotip)) {$this->Country="局域網";return ;}  
  83. $nRet;  
  84. $ip=$this->IpToInt ( $dotip );  
  85. $this->fp= fopen(__QQWRY__, "rb");  
  86. if ($this->fp == NULL) {  
  87. $szLocal"OpenFileError";  
  88. return 1;  
  89. @fseek ( $this->fp , 0 , SEEK_SET ); 
  90. $buf=fread ( $this->fp , 8 ); 
  91. $this->FirstStartIp=ord($buf[0]) + (ord($buf[1])*256) + (ord($buf[2])*256*256) + (ord($buf[3])*256*256*256); 
  92. $this->LastStartIp=ord($buf[4]) + (ord($buf[5])*256) + (ord($buf[6])*256*256) + (ord($buf[7])*256*256*256); 
  93. $RecordCountfloor(($this->LastStartIp - $this->FirstStartIp) / 7); 
  94. if ($RecordCount <= 1){  
  95. $this->Country="FileDataError";  
  96. fclose($this->fp) ;  
  97. return 2 ;  
  98. }  
  99. $RangB= 0;  
  100. $RangE$RecordCount;  
  101. // Match ...  
  102. while ($RangB < $RangE-1)  
  103. {  
  104. $RecNofloor(($RangB + $RangE) / 2); 
  105. $this->getStartIp ( $RecNo ) ;  
  106. if ( $ip == $this->StartIp )  
  107. {  
  108. $RangB=$RecNo ;  
  109. break ;  
  110. }  
  111. if ($ip>$this->StartIp)  
  112. $RangB$RecNo;  
  113. else 
  114. $RangE$RecNo;  
  115. }  
  116. $this->getStartIp ( $RangB ) ;  
  117. $this->getEndIp ( ) ;  
  118. if ( ( $this->StartIp <= $ip ) && ( $this->EndIp >= $ip ) ){  
  119. $nRet=0 ;  
  120. $this->getCountry ( ) ;  
  121. //這樣不太好..............所以..........  
  122. $this->Local=str_replace("area error""",$this->Local);  
  123. }  
  124. else{  
  125. $nRet=3 ;  
  126. $this->Country='未知' ;  
  127. $this->Local='' ;  
  128. }  
  129. fclose ( $this->fp );  
  130. $this->Country=preg_replace("/(CZ88.NET)|(純真網絡)/","局域網/未知",$this->Country); 
  131. $this->Local=preg_replace("/(CZ88.NET)|(純真網絡)/","局域網/未知",$this->Local);  
  132. //////////////看看 $nRet在上面的值是什么0和3,于是將下面的行注釋掉  
  133. return $nRet ;  
  134. //return "$this->Country $this->Local";#如此直接返回位置和國家便可以了  
  135. }  
  136. function IpToInt($Ip) {  
  137. $array=explode('.',$Ip);  
  138. $Int=($array[0] * 256*256*256) + ($array[1]*256*256) + ($array[2]*256) + $array[3]; 
  139. return $Int;  
  140. }  
  141. }  
  142. ?> 

實例演示如下:

 

 
  1. $QQWry=new QQWry; 
  2. $QQWry->QQWry("60.31.95.255"); 
  3. $country = $QQWry->Country; 
  4. echo $QQWry->Country.",".$QQWry->Local; 

希望本文所述對大家的php程序設計有所幫助。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 綦江县| 和林格尔县| 泉州市| 祥云县| 玉山县| 广平县| 清水河县| 广丰县| 潼关县| 祁连县| 弋阳县| 兴国县| 河东区| 闸北区| 长治县| 张北县| 神农架林区| 双桥区| 遵化市| 嘉义市| 安达市| 芜湖县| 竹北市| 石家庄市| 巴林左旗| 沛县| 大方县| 扎囊县| 石楼县| 龙井市| 西藏| 金阳县| 恩平市| 霍州市| 凤庆县| 寻乌县| 塔城市| 滕州市| 新乡县| 雷州市| 松滋市|