本文實例講述了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程序設計有所幫助。