Heim  >  Artikel  >  php教程  >  分享PHP小技巧 PHP获取中国IP段

分享PHP小技巧 PHP获取中国IP段

WBOY
WBOYOriginal
2016-06-13 11:09:381181Durchsuche

大家在学习了

去这个地址下载全球ip段http://ftp.apnic.net/apnic/dbase/data/country-ipv4.lst,里面cn是中国ip段范围

新建一个php文件叫cnip.php,PHP获取中国IP段具体代码示例:

  1.  ?php  
  2. $filefile = file('./country-ipv4.lst');  
  3. $handle = fopen('./cnip.txt', 'a');  
  4. if($handle) {  
  5. foreach ($file as $key => $val) {  
  6. if (strpos($val, '#') !== 0) {  
  7. $ipLines = explode(' ', $val);  
  8. if ($ipLines[6] == 'cn') {   
  9. fputs($handle, $ipLines[0]. '-'. $ipLines[2]. "n");   
  10. }  
  11. }  
  12. }   
  13. }  
  14. ?> 

打开cnip.txt 中国的ip段范围就完成了PHP获取中国IP段,
如果是在window环境下,将 fputs($handle, $ipLines[0]. '-'. $ipLines[2]. "n"); 中的"n"改为"rn"即可换行。


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn