這篇文章主要介紹了PHP獲取ip對應地區和使用網絡類型的方法,實例分析了php通過調用ip138數據庫獲取IP及網絡類型的技巧,需要的朋友可以參考下
本文實例講述了PHP獲取ip對應地區和使用網絡類型的方法。分享給大家供大家參考。具體分析如下:
這里測試的時候因為ip168網站禁止,所以試著在原有代碼上修改為ip138數據庫的數據調用,代碼如下:
- <?php
- //原程序有問題,現修改為ip138數據庫
- /**
- * 獲取IP地區
- * Enter description here ...
- * @param unknown_type $ip
- */
- function GetArea($ip){
- $url = "http://www.ip138.com/ips8.asp?ip=".$ip."&action=2";
- $contents = file_get_contents($url);
- preg_match_all('|<li>本站主數據:.*</li>|',$contents,$rsR);
- $rsR[0][0] = str_replace("<li>本站主數據:", "", $rsR[0][0]);
- $pos = strpos($rsR[0][0],'</li>');
- $Area = substr_replace($rsR[0][0],'',$pos);
- return $Area;
- }
- header('Content-type:text/html;Charset=gb2312');
- $area = GetArea('218.242.232.194');
- print_r($area);
- ?>
希望本文所述對大家的php程序設計有所幫助。
新聞熱點
疑難解答