php获取ip地址所在地查询程序,PHP中根据IP地址判

 代码如下

获得IP地址

<?php header("content-Type: text/html; charset=utf-8");$weburl=$_SERVER['SERVER_NAME'];$weblink=";

在 PHP 中收获当前媒体人的IP地址,依然相比较简单的:

<style type="text/css">
body {font-size:14px;line-height:150%;background-image: url(bg_body.jpg);background-repeat:repeat-x;}
a{ text-decoration:none; color:#990000}
.detect_table {border: 1px solid #C3E1EE;border-collapse: collapse;margin-top: 5px;width: 650px;}
</style>

$ip = $_SERVER['REMOTE_ADDR']

<SCRIPT LANGUAGE="JavaScript">function checkIP(){var ipArray,ip,j;ip = document.ipform.ip.value;if(/[A-Za-z_-]/.test(ip)){if(!/^([w-]+.)+((com)|(net)|(org)|(gov.cn)|(info)|(cc)|(com.cn)|(com.hk)|(net.cn)|(org.cn)|(com.ru)|(net.ru)|(org.ru)|(name)|(biz)|(hk)|(vn)|(com.sg)|(pk)|(ph)|(ac)|(bz)|(de)|(net.au)|(my)|(tv)|(us)|(jp)|(kr)|(sg)|(mn)|(cd)|(ue)|(sh)|(lc)|(hn)|(me)|(la)|(cc)|(tk)|(com.tw)|(cm)|(cn))$/.test(ip)){alert("不是科学的域名,请检查域名格式或看是或不是有空格"); document.ipform.ip.focus();return false;}}else{ipArray = ip.split(".");j = ipArray.length;if(j!=4){alert("不是不错的IP");document.ipform.ip.focus();return false;}for(var i=0;i<4;i++){if(ipArray[i].length==0 || ipArray[i]>255){alert("不是不容置疑的IP");document.ipform.ip.focus();return false;}}}}
google_ad_client = "ca-pub-2006512290696014";google_ad_slot = "6662313368";google_ad_width = 468;google_ad_height = 60;
</script>
</head>
<body>
<?php
$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');
?>
<div align="center" style="margin-top:100px">
  <p> </p>
  <p><a href="" title="<?=$weburl;?>IP查询介绍" target="_blank">IP查询</a></p>
</div>
 <div class="c" align="center">
 <div>
<FORM METHOD=POST ACTION="" name="ipform" onsubmit="return checkIP();">
<p>IP地址或域名:
<input type="text" name="ip" size="27" maxlength="100" title="<?=$weburl;?>提醒您在此输入ip或域名" id="url" value="<?php if(!$_POST['ip']){echo $urlip;}else{echo $_POST['ip'];}?>">
<input type="submit" value="查 询" id="sub"><INPUT TYPE="hidden" name="action" value="2"></p>
</FORM>
<p>
  <?php
 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: [<a href='".$weblink.$ip."'><font color=#FF0000>".$ip."</font></a>]IP详细地址: [<font color=#FF0000>".$ipaddress."</font>]<br/>";
 }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是[<a href='".$weblink.$ipdaili."'><font color=#FF0000>".$ipdaili."</font></a>]     来自".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 "<br /><br /><a href='."' target='_blank'>点击查看域名 ".$whois." 的WhoIs消息</a>";}
?>
</p>
</div>
</div>
<div align="center" style=" margin-top:150px">
<table class="detect_table" id="ip_all">
<tbody><tr><th>IP</th><th>国家/地区</th><th>省份</th><th>城市</th><th>县</th><th>运营商</th></tr>
<tr><td align="center"><span class="sip"><?=$ip;?></span></td><td align="center"><?=$ipdata->country.$ipdata->area?></td><td align="center"><?=$ipdata->region?></td><td align="center"><?=$ipdata->city?></td><td align="center"><?=$ipdata->county?></td><td align="center"><?=$ipdata->isp?></td></tr></tbody></table><br>
</body>
</html>
<?php
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="";
 $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;}
}
?>

上边IP一时获取不到实际IP地址大家能够如下操作

淘宝IP地址库接口说明

$ip = GetIP();

  1. 呼吁接口(GET):

将IP调换为都市等音信

]

Taobao提供了贰个IP数据接口:

  1. 一呼百应消息:

$response = file_get_contents(');
$result   = json_decode($response);
print_r($result);

(json格式的)国家 、省(自治区或直辖市)、市(县)、运维商

出口结果为:

  1. 回到数据格式:

stdClass Object
(
    [code] => 0
    [data] => stdClass Object
        (
            [country] => 中国
            [country_id] => CN
            [area] => 华南
            [area_id] => 800000
            [region] => 广东省
            [region_id] => 440000
            [city] => 深圳市
            [city_id] => 440300
            [county] =>
            [county_id] => -1
            [isp] => 电信
            [isp_id] => 100017
            [ip] => 183.16.191.102
        )
)

 代码如下

 

{"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"}}

function GetIP() {
if ($_SERVER["HTTP_X_FORWARDED_FOR"])
$ip = $_SERVER["HTTP_X_FORWARDED_FOR"];
else if ($_SERVER["HTTP_CLIENT_IP"])
$ip = $_SERVER["HTTP_CLIENT_IP"];
else if ($_SERVER["REMOTE_ADDR"])
$ip = $_SERVER["REMOTE_ADDR"];
else if (getenv("HTTP_X_FORWARDED_FOR"))
$ip = getenv("HTTP_X_FORWARDED_FOR");
else if (getenv("HTTP_CLIENT_IP"))
$ip = getenv("HTTP_CLIENT_IP");
else if (getenv("REMOTE_ADDR"))
$ip = getenv("REMOTE_ADDR");
else
$ip = "Unknown";
return $ip;
}
echo GetIP();

其间code的值的含义为,0:成功,1:战败。

 

.htaccess 通用版伪静态法规 别的法规请参谋上边包车型客车张开修改

 代码如下

RewriteEngine on
RewriteRule ^(.*)$ index.php?id=$1 [L]

sina SAE专版伪静态配置规则

 代码如下

- rewrite: if (!is_dir() && !is_file() && path ~ "/(.*)" ) goto "index.php?$1"

本文由365bet体育在线官网发布于关于计算机,转载请注明出处:php获取ip地址所在地查询程序,PHP中根据IP地址判

TAG标签:
Ctrl+D 将本页面保存为书签,全面了解最新资讯,方便快捷。