kn007的个人博客
♥ You are here: Home > 软件与网络 > php > [php&SAE]弄了个ip地址查询工具的页面

[php&SAE]弄了个ip地址查询工具的页面

by | 8 Comments

提醒:本文最后更新于 3932 天前,文中所描述的信息可能已发生改变,请仔细核实。


• 本IP地址查询为单文件php版,直接调用淘宝IP的地址库,非常的精确,只要淘宝IP地址库实时更新,不倒闭,那么获得的数据将会是最新的。
• 非SAE平台,请去除curl_file_get_contents函数,并修改ipdata中的curl_file_get_contents为file_get_contents即可。
• 此代码非常简单,只使用了SaeFetchurl函数,完全免费开源,请指出不足地方,帮助我改进。
• 演示地址:https://kn007.net/ip.php,或者通过主页导航栏GetIPAddr进入
• 其他待添加...

<?php header("content-Type: text/html; charset=utf-8");$weburl=$_SERVER['SERVER_NAME'];$weblink="http://".$weburl."/";if(count($_GET)>0){$urlip=array_keys($_GET);$urlip=str_replace("_",".",$urlip[0]);}?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<TITLE>IP地址查询 | kn007的个人博客</TITLE>

<style type="text/css">

body {font-size:16px;line-height:150%;background-color: #23282E;}

a{ text-decoration:none; color:#999;}

.detect_table {border: 3px solid #5692BC;border-collapse: collapse;margin-top: 5px;width: 650px; color:#5692BC;}

.c{ color:#5692BC;}

.ipfooter{line-height:24px;margin:0 auto; text-align:center; color:#999;font-family: Trebuchet MS, 'Lucida Grande',"Microsoft YaHei", Arial,sans-serif;}

.ipfooter a{ color:#999}

.ipfooter a:hover{ color:#ccc}

</style>

<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;}}}}

</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>&nbsp;</p>

<p><h2><a href="https://kn007.net/ip.php" title="IP查询查询" target="_blank"> IP地址查询</a></h2></p>

</div>

<div align="center">

<div>

<FORM METHOD=POST ACTION="" name="ipform" onsubmit="return checkIP();">

<p>IP地址或域名:

<input type="text" name="ip" size="27" maxlength="100" title="提醒您在此输入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;

$net=$_POST['ip']?$_POST['ip']:$urlip;

if($net){preg_match('/((w|-)+.)+[a-z]{2,4}/i',$net) ? $whois=str_replace("www.","",$net):"";}

if($whois){

echo "您查询的域名:&nbsp;[<a href='".$weblink."func/go.php?url=http://".$whois."'><font color=#5692BC>".$ip."</font></a>] 域名详细地址:&nbsp;[<font color=#5692BC>".$ipaddress."</font>]<br/>";

}else{

echo "您查询的IP:&nbsp;[<font color=#5692BC>".$ip."</font>] IP详细地址:&nbsp;[<font color=#5692BC>".$ipaddress."</font>]<br/>";

}

}else{echo "囧,你输入的IP,居然不能查询到!不是输入错误?";}

$ipdaili=$_SERVER['REMOTE_ADDR'];

if(!in_array($ipdaili,array('127.0.0.1',$ip,$_SERVER['SERVER_ADDR'],get_real_ip()))){

echo "您的代理IP是[<font color=#5692BC>".$ipdaili."</font>] 代理IP详细地址:&nbsp;[<font color=#5692BC>".ipdata($ipdaili)."</font>]<br/>";

}

?>

</p>

</div>

</div>

<div align="center" style=" margin-top:80px">

<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>

<div align="center">

<?php

echo "<a href='".$weblink."'>kn007的个人博客 &copy 2012 版权所有</a><br/><a href='".$weblink."func/go.php?url=".$weblink."'>Go back home!</a>";

?>

</div>

</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="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;}

}

?>

转载请注明转自:kn007的个人博客的《[php&SAE]弄了个ip地址查询工具的页面

donate
有所帮助?
Tags: , ,

Comments

8 Comments立即评论
  1. 回复

    :o :o :o 不错,支持。

    比较准,速度快,无广告! :lol:

    1. MOD回复

      @vbasmtt: 谢谢肯定

    2. MOD回复

      @vbasmtt: 接口已采用sina

  2. 回复

    不错,搞的挺准的 。支持下 。

  3. 感觉淘宝的IP库有所偏差,尤其是判断国外IP的时候,ipip.net数据似乎更精确一些。

    1. MOD回复

      @小z博客: 我这个是复合数据,跟ipip的差不多

  4. 回复

    138行出现错误

    1. MOD回复

      @138: 这么老的帖子,已经不在SAE了。
      138行是$f = new SaeFetchurl();是吧,不行就看看最新文档,看函数改成啥了。

icon_wink.gificon_neutral.gificon_mad.gificon_twisted.gificon_smile.gificon_eek.gificon_sad.gificon_rolleyes.gificon_razz.gificon_redface.gificon_surprised.gificon_mrgreen.gificon_lol.gificon_idea.gificon_biggrin.gificon_evil.gificon_cry.gificon_cool.gificon_arrow.gificon_confused.gificon_question.gificon_exclaim.gif