利用淘宝ip库限制地区访问

利用淘宝ip库限制地区访问

有些应用可能需要对某些地区的用户进行限制访问.在采用才此方法的时候,可以利用一些ip库对访问者的ip进行判断,从而对相应地区的用户进行对应的操作

淘宝ip库地址:淘宝ip库

下面的实例仅是限制了只能安徽省的用户访问,需要的用户可以进行相应的修改

$verification = '安徽省';//需要屏蔽省份的IP    
$ip = $_SERVER['REMOTE_ADDR'];//获取访客IP    
$antecedents = $_SERVER['HTTP_REFERER'];//访客来路地址    
$result = file_get_contents("http://ip.taobao.com/service/getIpInfo.php?ip=".$ip);//IP数据库来自淘宝。    
$address = json_decode($result,true);    
//判断访客是否属于XX省,是否来自百度,是否来自谷歌    
if($address['data']['region'] != $verification && strpos($antecedents, 'baidu') === false && strpos($antecedents, 'google') === false){    
echo "仅限安徽省用户访问";    
exit();    
}else{    
echo "<script>window.location.href='/';</script>";    
exit;    
}  

原文来自:http://www.51mapleth.com/541.html

附文:从APNIC提取IP信息
保存成xx.sh的shell文件

#!/bin/bash  
# download from apnic  
rm -f delegated-apnic-latest  
wget http://ftp.apnic.net/apnic/stats/apnic/delegated-apnic-latest  
# IPs allocated to china.  
grep 'apnic|CN|ipv4|' delegated-apnic-latest | cut -f 4 -d'|' > delegated-apnic-CN  
# get detail of echo IP from apnic database.  
rm -f apnic_CN.txt  
while read ip  
do  
# query apnic database  
echo "query who is $ip"  
whois -h whois.apnic.net $ip > tmp.txt  
grep inetnum tmp.txt >> apnic_CN.txt # IP range  
grep netname tmp.txt >> apnic_CN.txt # netname which include sp information   
grep descr tmp.txt >> apnic_CN.txt # description which include province information  
echo "" >> apnic_CN.txt   
done < delegated-apnic-CN  
# clean up  
rm -f tmp.txt  
rm -f delegated-apnic-latest  
rm -f delegated-apnic-CN  

利用淘宝ip库限制地区访问

原文链接:,转发请注明来源!

发表评论