PHP Get IP Address Location Query Program_PHP Tutorial

WBOY
Release: 2016-07-13 16:56:12
Original
1411 people have browsed it

文章利用了第三方接口来实现ip地址查询,这里根据用户提交的IP地址就可以方便的查出IP地址真实所在地。

 代码如下 复制代码

0){$urlip=array_keys($_GET);$urlip=str_replace("_",".",$urlip[0]);}?>




$ip=$_POST['ip']?$_POST['ip']:$urlip;if(!$ip)$ip=get_real_ip();
$ip=preg_match('/((w|-)+.)+[a-z]{2,4}/i',$ip)?gethostbyname($ip) : $ip;
$ipdata=ipdata($ip,'all');
?>


 


 

IP查询



 

 


IP地址或域名:




  if(is_ip($ip)){
$ipaddress=$ipdata->country;
  if($ipdata->region==$ipdata->city){$ipaddress.=$ipdata->city;}else{$ipaddress.=$ipdata->region.$ipdata->city;}
  $ipaddress.=$ipdata->isp;
  echo "您查询的IP: [".$ip."]IP详细地址: [".$ipaddress."]
";
 }else{echo "?澹?闶淙氲?P,居然不能查询到!不是输入错误?";}
 $ipdaili=$_SERVER['REMOTE_ADDR'];
 if(!in_array($ipdaili,array('127.0.0.1',$ip,$_SERVER['SERVER_ADDR'],get_real_ip()))){
  echo "您的代理IP是[".$ipdaili."] 来自".ipdata($ipdaili)."";
 }
$whiosip=$_POST['ip']?$_POST['ip']:$urlip;
if($whiosip){preg_match('/((w|-)+.)+[a-z]{2,4}/i',$whiosip) ? $whois=str_replace("www.","",$whiosip):"";}
if($whois){echo "

点击查看域名 ".$whois." 的WhoIs信息";}
?>







IP国家/地区省份城市运营商
country.$ipdata->area?>region?>city?>county?>isp?>




function curl_file_get_contents($durl){
  $f = new SaeFetchurl();
   $content = $f->fetch($durl);
   if($f->errno() == 0)  $r=$content;
   else $r=$f->errmsg();
   return $r;
}
function get_real_ip(){
 if(getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown'))
 {$ip = getenv('HTTP_CLIENT_IP');}
 elseif(getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown'))
 {$ip = getenv('HTTP_X_FORWARDED_FOR');}
 elseif(getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown'))
 {$ip = getenv('REMOTE_ADDR');}
 elseif(isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown'))
 {$ip = $_SERVER['REMOTE_ADDR'];}
 return preg_match("/[d.]{7,15}/", $ip, $matches) ? $matches[0] : false;
}
function is_ip($str) {
    $ip = explode(".", $str);if (count($ip)<4 || count($ip)>4) return 0;
    foreach($ip as $ip_addr) {if ( !is_numeric($ip_addr) ) return 0;if ( $ip_addr<0 || $ip_addr>255 ) return 0;
    }return 1;
}
function ipdata($ip,$name="0")
{
 $url="http://ip.taobao.com/service/getIpInfo.php?ip=";
 $data=json_decode(curl_file_get_contents($url.$ip)); $ipdata=$data->data;
 if($name=="all")   {return $ipdata;}
 else if($name=="0"){return $ipdata->country.$ipdata->region.$ipdata->city.$ipdata->isp;}
 else if($name=="1"){return $ipdata->country;}
 else if($name=="2"){return $ipdata->area;}
 else if($name=="3"){return $ipdata->region;}
 else if($name=="4"){return $ipdata->city;}
 else if($name=="5"){return $ipdata->county;}
 else if($name=="6"){return $ipdata->isp;}
}
?>

Taobao IP address library interface description
1. Request interface (GET):

http://ip.taobao.com/service/getIpInfo.php?ip=[ip address string]

2. Response information:

(json format) country, province (autonomous region or municipality), city (county), operator

3. Return data format:

The code is as follows
 代码如下 复制代码
{"code":0,"data":{"ip":"210.75.225.254","country":"u4e2du56fd","area":"u534eu5317",
"region":"u5317u4eacu5e02","city":"u5317u4eacu5e02","county":"","isp":"u7535u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}
Copy code

{"code":0,"data" :{"ip":"210.75.225.254","country":"u4e2du56fd","area":"u534eu5317",

"region":"u5317u4eacu5e02","city":"u5317u4eacu5e02","county":"","isp":"u7535u4fe1",

"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",

"county_id":"-1","isp_id":"100017"}}

 代码如下 复制代码
RewriteEngine on
RewriteRule ^(.*)$ index.php?id= [L]

 代码如下 复制代码
- rewrite: if (!is_dir() && !is_file() && path ~ "/(.*)" ) goto "index.php?"
The meaning of the value of code is, 0: success, 1: failure.
.htaccess Universal version of pseudo-static rules. For other rules, please refer to the following for modifications

The code is as follows
Copy code
RewriteEngine on RewriteRule ^(.*)$ index.php?id=$1 [L] sina SAE special edition pseudo-static configuration rules
http://www.bkjia.com/PHPjc/631621.html
www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631621.htmlTechArticleThe article uses a third-party interface to implement IP address query. Here, you can easily check based on the IP address submitted by the user. Show the true location of the IP address. The code is as follows Copy the code ?php header(c...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template