后退 刷新 前进 收藏 顶部 |   知道 黑匣子 富媒体 技术服务

ip 区域限制访问的js方法

程序猿 JS ip 限制 访问 js... (shuke.2013-07-31 08:55)
有时候因为特殊原因,网站只允许某一地区访问或者禁止某一地区访问!
方法有很多种:
1.在防火墙上过滤
2.在IIS/Apache/Nginx等Web服务软件上配置
3.在网站上验证
这里向大家介绍一个简单的方法,基于方法3。
那就是比较通用的js代码来实现。
IP库用的是新浪的。
用返回值来判断地区,但是还是有不完善的地方。
新浪的IP库:
<script language="javascript" type="text/javascript" src="http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js"></script>
<script type="text/javascript">
//var remote_ip_info = {"ret":1,"start":-1,"end":-1,"country":"\u4e2d\u56fd","province":"\u6cb3\u5357","city":"\u90d1\u5dde","district":"","isp":"","type":"","desc":""};
var add0 = remote_ip_info.country;
var add1 = remote_ip_info.province;
var add2 = remote_ip_info.city;
var add = add0+add1+add2;


document.writeln(add);
if( add1 == "河南"){
document.writeln("this is henan(河南)");
}else{
document.writeln("this is not henan(河南)");
}

</script>


2 楼 shuke[2013-07-31 15:32]
<script src="http://counter.sina.com.cn/ip" type="text/javascript" charset=gb2312></script>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<script type="text/javascript" charset=gb2312>
var sf=ILData[2];
if(sf.indexOf("北京")>=0 || sf.indexOf("上海")>=0 || sf.indexOf("保留")>=0 ){

编码问题一定要注意,都要是GB2312才可以对比出来。

以上JS新浪不在使用
3 楼 shuke[2013-09-04 15:21]
在实战中发现还有地方有不完善的地方,代码改过后
<!-- IP STOP START -->
<script src="http://counter.sina.com.cn/ip" type="text/javascript" charset=gb2312></script>
<script type="text/javascript" charset="gb2312">
var sf=ILData[1];
var sf2=ILData[2];
if(sf.indexOf("上海")>=0 || sf2.indexOf("上海")>=0 || sf.indexOf("保留")>=0 ){
        window.location.href="/404.html";
} 
</script>
<!-- IP OVER -->


4 楼 shuke[2014-04-28 14:42]

1、淘宝IP地址库    http://ip.taobao.com/

*.  请求接口(GET):http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址]

*.  返回数据格式:(json格式的)国家 、省(自治区或直辖市)、市(县)、运营商。例如:

{"code":0,"data":{"ip":"210.75.225.254","country":"\u4e2d\u56fd","area":"\u534e\u5317",
"region":"\u5317\u4eac\u5e02","city":"\u5317\u4eac\u5e02","county":"","isp":"\u7535\u4fe1",
"country_id":"86","area_id":"100000","region_id":"110000","city_id":"110000",
"county_id":"-1","isp_id":"100017"}}

注:其中code的值的含义为,0:成功,1:失败。

*.  淘宝IP接口使用示例PHP版:

<?php
$ip=get_ip_data();
function get_ip_data(){
	$ip=file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".get_client_ip());
	$ip = json_decode($ip);
	if($ip->code){
		return false;
	}
	$data = (array) $ip->data;
	print_r($data);//return $data;
}

//取客户端 ip
function get_client_ip(){
	if (isset($_SERVER['HTTP_CLIENT_IP']) and !empty($_SERVER['HTTP_CLIENT_IP'])){
		return $_SERVER['HTTP_CLIENT_IP'];
	}
	if (isset($_SERVER['HTTP_X_FORWARDED_FOR']) and !empty($_SERVER['HTTP_X_FORWARDED_FOR'])){
		return strtok($_SERVER['HTTP_X_FORWARDED_FOR'], ',');
	}
	if (isset($_SERVER['HTTP_PROXY_USER']) and !empty($_SERVER['HTTP_PROXY_USER'])){
		return $_SERVER['HTTP_PROXY_USER'];
	}
	if (isset($_SERVER['REMOTE_ADDR']) and !empty($_SERVER['REMOTE_ADDR'])){
		return $_SERVER['REMOTE_ADDR'];
	} else {
		return "0.0.0.0";
	}
}
?>

2、搜狐IP地址查询接口(默认编码GBK)http://pv.sohu.com/cityjson?ie=utf-8

3、新浪IP地址查询接口http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip=[ip地址]

注:参数format可设置返回格式:js/json;参数ip可选查询IP;

*.  返回格式如下:

var remote_ip_info = {"ret":1,"start":"125.40.0.0","end":"125.40.111.255","country":"\u4e2d\u56fd","province":"\u6cb3\u5357","city":"\u90d1\u5dde","district":"","isp":"\u8054\u901a","type":"","desc":""};

下面的实例用到了JQuery下的getScript()方法

<div id="sina_ip_info"></div>
<script src="http://code.jquery.com/jquery-1.7.2.min.js" type="text/javascript"></script>
<script type="text/javascript">
var uip = '125.40.0.0';
$.getScript('http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js&ip='+uip, function(_result){
	var ipData = ""; //初始化保存内容变量
	if (remote_ip_info.ret == '1'){
		ipData += "IP 详细信息:<br>";
		ipData += "IP:" + uip + "<br>";
		ipData += "国家:" + remote_ip_info.country + "<br>";
		ipData += "省份:" + remote_ip_info.province + "<br>";
		ipData += "城市:" + remote_ip_info.city + "<br>";
		ipData += "区:" + remote_ip_info.district + "<br>";
		ipData += "ISP:" + remote_ip_info.isp + "<br>";
		ipData += "类型:" + remote_ip_info.type + "<br>";
		ipData += "其他:" + remote_ip_info.desc + "<br>";
		$("#sina_ip_info").html(ipData); //显示处理后的数据
	} else {
		alert('错误', '没有找到匹配的 IP 地址信息!');
	}
});
</script>
5 楼 shuke[2014-04-28 15:03]
搜狐IP地址查询接口(默认编码GBK)
<script src="http://pv.sohu.com/cityjson" type="text/javascript" charset="gb2312"></script>
<script type="text/javascript"charset="gb2312">
var City = returnCitySN;
sf=City["cname"];
document.write(sf);
if(sf.indexOf("河南")>=0){
window.location.href="/404.html";
}
//alert(City["cname"]);
</script>

eg2
<script type="text/javascript"charset="gb2312">
$.getScript('http://pv.sohu.com/cityjson', function(_result){
var City = returnCitySN;
sf=City["cname"];
if(sf.indexOf("河南")>=0){
//window.location.href="/404.html";
alert(sf);
}
})
</script>

eg3
<script type="text/javascript"charset="gb2312">
function sk_ipstop(skip){
stopid=skip;
$.getScript('http://pv.sohu.com/cityjson', function(_result){
var City = returnCitySN;
sf=City["cname"];
if(sf.indexOf(stopid)>=0){
//window.location.href="/404.html";
alert(sf);
}
})
}
sk_ipstop("河南");
</script>



6 楼 shuke[2014-07-10 15:17]
php利用淘宝IP库获取用户ip地理位置
<?php
function getCity($ip)
{
$url="http://ip.taobao.com/service/getIpInfo.php?ip=".$ip;
$ipinfo=json_decode(file_get_contents($url));
if($ipinfo->code=='1'){
return false;
}
$city = $ipinfo->data->region.$ipinfo->data->city;
return $city;
}
header("Content-Type:text/html;charset=utf-8");
$skip=$_SERVER['REMOTE_ADDR'];
var_dump(getCity($skip));
echo "<BR>";
echo getCity($skip);
?>


7 楼 shuke[2014-09-29 14:47]
<!-- IP STOP START -->
<script src="http://counter.sina.com.cn/ip" type="text/javascript" charset="gb2312"></script>
<script type="text/javascript" charset="gb2312">
var sf=ILData[2];
var sf2=ILData[3];
var myDate = new Date();
skdata=myDate.getHours();
//document.write(sf2);
if (skdata>=8 && skdata<=18){
if(sf.indexOf("郑州")>=0 || sf2.indexOf("郑州")>=0){window.location.href="/404.html";}
}
</script>
<!-- IP OVER -->

现在加入一个加强版本(TIME+IP)的;
进度:100% 返回程序猿

解决织梦图集中上传图片时跳出302错误

图片新热点

导航


目录


    站内搜索
    首页 | W3C | ME