First use php to get the local IP, and then use php’s curl function to get the user’s detailed area. Let’s take a look at the code;
The code is as follows |
|
/*
* This function is to obtain the client and IP
代码如下 |
|
/*
* 这个函数是获取客户端和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;
}
|
*/
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 1, using the QQWry.Dat IP library, we only have chubby
Use the simple method.
Usage example
Example 1:
代码如下 |
|
$IpLocation = new IpLocation();
$client = $IpLocation->getlocation();
print_r($client);
|
The code is as follows |
|
$IpLocation = new IpLocation();
$client = $IpLocation->getlocation();
print_r($client);
|
Example 2:
代码如下 |
|
$IpLocation = new IpLocation('../qqwry/QQWry.Dat');
$client = $IpLocation->getlocation('115.148.101.72');
print_r($client);
|
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 are not introduced here. Let’s search them on Baidu.
Method 2, use api interface
Example 1,
The code is as follows |
|
/**
* Get IP area
* Enter description here ...
* @param unknown_type $ip
*/
function GetArea($ip){
代码如下 |
|
/**
* 获取IP地区
* 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('/ )/',$contents,$rs);
preg_match_all('|本站主数据:.* |',$contents,$rsR);
$rsR[0][0] = str_replace("本站主数据:", "", $rsR[0][0]);
$rsR[0][0] = str_replace(" ", "", $rsR[0][0]);
return $rsR[0][0];
}
|
$url = "http://ip168. com/ip/?ip=".$ip;
$contents = file_get_contents($url);
// preg_match_all('/ )/',$contents,$rs);
preg_match_all('|Main data of this site: .* |',$contents,$rsR);
$rsR[0][0] = str_replace("Main data of this site:", "", $rsR[0][0]);
$rsR[0][0] = str_replace(" ", "", $rsR[0][0]);
return $rsR[0][0];
}
|
Example 2,
The following function uses PHP’s curl function to obtain detailed regions from the Internet
代码如下 |
|
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("/ |
The code is as follows |
|
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("/ - (.*?)
/i",$ipdz,$jgarray);
preg_match("/this www.111cn.net website owner data: (.*)/i", $jgarray[1],$ipp);
return " Welcome from ".$ipp[1 ]." Friends! ";
}
echo lazdf(GetIP());// Output ip
|
http://www.bkjia.com/PHPjc/737679.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/737679.htmlTechArticleFirst use php to get the local ip, and then use php's curl function to get the user's detailed area. Next Take a look at the code; the code is as follows /* * This function is to get the client and IP */ function GetIP()...