Home  >  Article  >  Backend Development  >  PHP sample code to determine region name information based on IP_PHP tutorial

PHP sample code to determine region name information based on IP_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:37:08993browse

Look at the code

Copy the code The code is as follows:

header("Content-type: text/ html; charset=utf-8");
function getIP(){
if (isset($_SERVER)) {
if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) {
$realip = $_SERVER[HTTP_X_FORWARDED_FOR];
} elseif (isset($_SERVER[HTTP_CLIENT_IP])) {
$realip = $_SERVER[HTTP_CLIENT_IP];
} else {
$realip = $_SERVER[ REMOTE_ADDR];
}
} else {
if (getenv("HTTP_X_FORWARDED_FOR")) {
$realip = getenv( "HTTP_X_FORWARDED_FOR");
} elseif (getenv("HTTP_CLIENT_IP" )) {
$realip = getenv("HTTP_CLIENT_IP");
} else {
$realip = getenv("REMOTE_ADDR");
}
}
return $realip;
}

$ip = getIP();

// Use Sina interface to query regional information based on IP
/* $res0 = file_get_contents("http://int .dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=$ip");
$res0 = json_decode($res0);
print_r($res0);
echo "
"; */

// Use Taobao interface to query regional information based on IP
$res1 = file_get_contents("http://ip.taobao.com/service/getIpInfo. php?ip=$ip");
$res1 = json_decode($res1);
/* print_r($res1); */

How to read data, see below .
Copy code The code is as follows:

$array = get_object_vars($res1);//Assign value to array
foreach ($array as $value){
echo $value->region."
";
echo $value->city."
";
echo $value->ip."
";
}
?>

//Or use object data to access echo $res1->data-> ;city;

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/736815.htmlTechArticleLook at the code and copy the code. The code is as follows: ?php header("Content-type: text/html; charset=utf- 8"); function getIP(){ if (isset($_SERVER)) { if (isset($_SERVER[HTTP_X_FORWARDED_FOR])) { $re...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn