惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Forbes - Security
Forbes - Security
T
Tailwind CSS Blog
Hugging Face - Blog
Hugging Face - Blog
Blog — PlanetScale
Blog — PlanetScale
WordPress大学
WordPress大学
aimingoo的专栏
aimingoo的专栏
Y
Y Combinator Blog
U
Unit 42
I
InfoQ
CTFtime.org: upcoming CTF events
CTFtime.org: upcoming CTF events
V
Visual Studio Blog
B
Blog RSS Feed
Vercel News
Vercel News
F
Fortinet All Blogs
Know Your Adversary
Know Your Adversary
T
Troy Hunt's Blog
博客园 - 【当耐特】
MongoDB | Blog
MongoDB | Blog
大猫的无限游戏
大猫的无限游戏
A
About on SuperTechFans
Jina AI
Jina AI
小众软件
小众软件
T
Threatpost
有赞技术团队
有赞技术团队
人人都是产品经理
人人都是产品经理
The Hacker News
The Hacker News
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Microsoft Azure Blog
Microsoft Azure Blog
Recent Announcements
Recent Announcements
酷 壳 – CoolShell
酷 壳 – CoolShell
Scott Helme
Scott Helme
B
Blog
腾讯CDC
Last Week in AI
Last Week in AI
P
Proofpoint News Feed
S
Schneier on Security
N
News and Events Feed by Topic
Microsoft Security Blog
Microsoft Security Blog
K
Kaspersky official blog
G
Google Developers Blog
T
Tor Project blog
PCI Perspectives
PCI Perspectives
S
Secure Thoughts
Google Online Security Blog
Google Online Security Blog
Latest news
Latest news
Google DeepMind News
Google DeepMind News
MyScale Blog
MyScale Blog
罗磊的独立博客

博客园 - senly

金财微报表的技术支持服务 党建积分的技术支持服务 IE6的position:fixed PHP实现前台同步显示后台任务进度 css秘籍:复选框与文字对齐的问题 php遍历数组 list foreach each方法总结 jQuery cookie 购物车及其实现 CSS自定义滚动条样式 iframe 父窗口和子窗口相互的调用方法集锦 44种IE css bug实例测试总结 IE6不支持position:fixed的解决方法 jQuery精仿手机上的翻牌效果菜单 jquery菜单左右翻屏效果 jQuery实现无限循环滚动公告 Load JSON data with jQuery, PHP and MySQL php把从数据库读取出来的数据存放到数组里 mysql 实现行号的方法——如何获取当前记录所在行号 DedeCMS会员排行调用代码,实现连接到会员空间 程序员们 不要想一辈子靠技术混饭吃
php根据ip查询所在地区(非常有用,赶集网就用到)
senly · 2013-03-07 · via 博客园 - senly

dat文件,关于ip对应地区的信息文件

qqwry.dat文件

网上自己下载

class类文件,解析qqwry.data文件的

IpLocation.php文件

  1. <?php  
  2. class IpLocation {  
  3.      
  4.  
  5.   
  6.     private $fp;  
  7.   
  8.      
  9.  
  10.  
  11.   
  12.     private $firstip;  
  13.   
  14.      
  15.  
  16.  
  17.   
  18.     private $lastip;  
  19.   
  20.      
  21.  
  22.  
  23.   
  24.     private $totalip;  
  25.   
  26.      
  27.  
  28.  
  29.  
  30.   
  31.     public function __construct($filename = "qqwry.dat") {  
  32.         $this->fp = 0;  
  33.         if (($this->fp = @fopen($filename'rb')) !== false) {  
  34.             $this->firstip = $this->getlong();  
  35.             $this->lastip = $this->getlong();  
  36.             $this->totalip = ($this->lastip - $this->firstip) / 7;  
  37.         }  
  38.     }  
  39.   
  40.      
  41.  
  42.  
  43.  
  44.   
  45.     public function getlong() {  
  46.           
  47.         $result = unpack('Vlong'fread($this->fp, 4));  
  48.         return $result['long'];  
  49.     }  
  50.   
  51.      
  52.  
  53.  
  54.  
  55.  
  56.   
  57.     public function getlong3() {  
  58.           
  59.         $result = unpack('Vlong'fread($this->fp, 3).chr(0));  
  60.         return $result['long'];  
  61.     }  
  62.   
  63.      
  64.  
  65.  
  66.  
  67.  
  68.  
  69.   
  70.     public function packip($ip) {  
  71.           
  72.           
  73.         return pack('N'intval(ip2long($ip)));  
  74.     }  
  75.   
  76.      
  77.  
  78.  
  79.  
  80.  
  81.  
  82.   
  83.     public function getstring($data = "") {  
  84.         $char = fread($this->fp, 1);  
  85.         while (ord($char) > 0) {   
  86.             $data .= $char  
  87.             $char = fread($this->fp, 1);  
  88.         }  
  89.         return mb_convert_encoding($data'utf-8''gb2312');  
  90.     }  
  91.   
  92.      
  93.  
  94.  
  95.  
  96.  
  97.   
  98.     public function getarea() {  
  99.         $byte = fread($this->fp, 1);   
  100.         switch (ord($byte)) {  
  101.             case 0:   
  102.                 $area = "";  
  103.             break;  
  104.             case 1:  
  105.             case 2:   
  106.                 fseek($this->fp, $this->getlong3());  
  107.                 $area = $this->getstring();  
  108.             break;  
  109.             default  
  110.                 $area = $this->getstring($byte);  
  111.             break;  
  112.         }  
  113.         return $area;  
  114.     }  
  115.   
  116.      
  117.  
  118.  
  119.  
  120.  
  121.   
  122.     function getlocation($ip) {  
  123.         if (!$this->fp) return null;   
  124.         $location['ip'] = gethostbyname($ip);   
  125.         $ip = $this->packip($location['ip']);   
  126.           
  127.           
  128.         $l = 0;   
  129.         $u = $this->totalip;   
  130.         $findip = $this->lastip;   
  131.         while ($l <= $u) {   
  132.             $i = floor(($l + $u) / 2);   
  133.             fseek($this->fp, $this->firstip + $i * 7);  
  134.             $beginip = strrev(fread($this->fp, 4));   
  135.               
  136.               
  137.             if ($ip < $beginip) {   
  138.                 $u = $i - 1;   
  139.             }else{  
  140.                 fseek($this->fp, $this->getlong3());  
  141.                 $endip = strrev(fread($this->fp, 4));   
  142.                 if ($ip > $endip) {   
  143.                     $l = $i + 1;   
  144.                 }else  
  145.                     $findip = $this->firstip + $i * 7;  
  146.                     break  
  147.                 }  
  148.             }  
  149.         }  
  150.   
  151.           
  152.         fseek($this->fp, $findip);  
  153.         $location['beginip'] = long2ip($this->getlong());   
  154.         $offset = $this->getlong3();  
  155.         fseek($this->fp, $offset);  
  156.         $location['endip'] = long2ip($this->getlong());   
  157.         $byte = fread($this->fp, 1);   
  158.   
  159.         switch (ord($byte)) {  
  160.             case 1:   
  161.                 $countryOffset = $this->getlong3();   
  162.                 fseek($this->fp, $countryOffset);  
  163.                 $byte = fread($this->fp, 1);   
  164.                 switch (ord($byte)) {  
  165.                     case 2:   
  166.                         fseek($this->fp, $this->getlong3());  
  167.                         $location['country'] = $this->getstring();  
  168.                         fseek($this->fp, $countryOffset + 4);  
  169.                         $location['area'] = $this->getarea();  
  170.                         break;  
  171.                     default  
  172.                         $location['country'] = $this->getstring($byte);  
  173.                         $location['area'] = $this->getarea();  
  174.                         break;  
  175.                 }  
  176.                 break;  
  177.             case 2:   
  178.                 fseek($this->fp, $this->getlong3());  
  179.                 $location['country'] = $this->getstring();  
  180.                 fseek($this->fp, $offset + 8);  
  181.                 $location['area'] = $this->getarea();  
  182.                 break;  
  183.             default  
  184.                 $location['country'] = $this->getstring($byte);  
  185.                 $location['area'] = $this->getarea();  
  186.                 break;  
  187.         }  
  188.         if ($location['country'] == " CZ88.NET") {   
  189.             $location['country'] = "未知";  
  190.         }  
  191.         if ($location['area'] == " CZ88.NET") {  
  192.             $location['area'] = "";  
  193.         }  
  194.         return $location;  
  195.     }  
  196.   
  197.       
  198.      
  199.  
  200.  
  201.   
  202.     function __desctruct() {  
  203.         if ($this->fp) {  
  204.             fclose($this->fp);  
  205.         }  
  206.         $this->fp = 0;  
  207.     }  
  208. }  
  209. ?>   

这个也可以网上下载,也可以copy这里的,这里的也是很全的。

执行文件,我这里叫ip_location.php文件

    1. <?php   
    2. function getIpPlace(){  
    3.     require_once("IpLocation.php")  
    4.     $ipfile = "qqwry.dat";        
    5.     $iplocation = new IpLocation($ipfile);    
    6.     $ipresult = $iplocation->getlocation("ip地址");   
    7.     return $ipresult;  
    8. }  
    9. print_r($getIpPlace());   
    10. ?> 

http://blog.csdn.net/motian06/article/details/7919675