First use PHP to obtain the IP, and then use the PHP Curl function to obtain the user's detailed area. Here's a look at the code;
The code is as follows |
|
/* * This function is to get the client and IP
*/ function GetIP () { if (!empty ($_server["HTTP_CLIENT_IP"])) { $cip = $_server["Http_client_ip"]; } else if (!empty ($_server["http_x_forwarded_for"])) { $cip = $_server["Http_x_forwarded_for"]; } else if (!empty ($_server["REMOTE_ADDR"])) { $cip = $_server["REMOTE_ADDR"]; } Else { $cip = '; } Preg_match ("/[d.") {7,15}/", $cip, $cips); $cip = isset ($cips [0])? $cips [0]: ' Unknown '; Unset ($cips); return $CIP; } |
Method one, using QQWry.Dat IP library, we only fat
In a simple way.
Using the sample
Example one:
The code is as follows |
|
$IpLocation = new Iplocation (); $client = $IpLocation->getlocation (); Print_r ($client); |
Example two:
The code is as follows |
|
$IpLocation = new Iplocation ('.. /qqwry/qqwry.dat '); $client = $IpLocation->getlocation (' 115.148.101.72 '); Print_r ($client); |
The specific IP library and Iplocation class files here do not introduce everyone Baidu search bar.
Method two, using API interface
Example One,
The code is as follows |
|
/** * Get IP area * Enter description here ... * @param unknown_type $ip */ function Getarea ($IP) { $url = "http://ip168.com/ip/?ip=". $ip; $contents = file_get_contents ($url); Preg_match_all ('/Preg_match_all (' |<div> site main data:. *</div>| ', $contents, $rsR); $rsR [0][0] = Str_replace ("<div> site master data:", "", $rsR [0][0]); $rsR [0][0] = Str_replace ("</div>", "", $rsR [0][0]); return $rsR [0][0]; } |
Example Two,
The following function uses PHP's curl function to get detailed area from the Web
code is as follows |
&nbs P; |
function LAZDF ($IP) { $curl = Curl_init (); Curl_setopt ($curl, Curlopt_url, "http://www.ip138.com/ips138.asp?ip=". $ip); curl_setopt ($curl, curlopt_returntransfer,1); $ipdz =curl_exec ($curl); Curl_close ($curl); Preg_match (/<ul class= "Ul1" ><li> (. *?) </li>/i ", $ipdz, $jgarray); Preg_match ("/www.111cn.net station main data: (. *)/I", $jgarray [1], $IPP); return "<div class=" global_widht global_zj zj "style=" Background:none repeat the scroll 0% 0% rgb (226, 255, 191); font-size:12px; Color:rgb (85, 85, 85); height:30px; line-height:30px; BORDER-BOTTOM:1PX Solid RGB (204, 204, 204); Text-align:left; " > welcome from <b> "$ipp [1]." </b> Friends! </div> "; } Echo lazdf (GeTIP ());/output IP |