屏蔽国内IP,访问需要进行身份验证解决思路

WBOY
Release: 2016-06-13 13:33:36
Original
1449 people have browsed it

屏蔽国内IP,访问需要进行身份验证
有一外贸网站想屏蔽国内IP,但我们自己需要管理和允许特定的国内用户访问所以要进行身份验证,请问这样的功能要怎样实现呢?第一次发帖,请大家多关注!谢谢!

------解决方案--------------------
可以做个页面,在用户访问时,
1.获取用户的IP
2.然后到开放的IP接口去查询用户的地理位置(例如新浪、网易、ip138,都有开放的ip查询接口)
3.有了用户的地理位置,再决定是屏蔽之,还是验证之
------解决方案--------------------

PHP code

function sina($ip)
{
    $php_runtime = microtime(TRUE);
    $str = file_get_contents("http://int.dpool.sina.com.cn/iplookup/iplookup.php?ip=".$ip);
    $tmp = explode("    ",$str);
    (isset($tmp[4]) && !empty($tmp[4])) ? $location['country'] = $tmp[4] : $location['country'] = null;
    (isset($tmp[5]) && !empty($tmp[5])) ? $location['city'] = $tmp[5] : $location['city'] = null;
    (empty($tmp[6]) || !preg_match("/电/",$str) || !preg_match("/通/",$str)) ? $location['sp'] = $tmp[7] : $location['sp'] = $tmp[6];

    $location['time'] = microtime(TRUE) - $php_runtime;
    if(!empty($location))
        return $location;
    else
        return false;
}
<br><font color="#e78608">------解决方案--------------------</font><br>通过用户来访的IP地址可以算出该IP属于哪个子网,如110.80.0.0/13<br>将全中国可划分的子网存在一个数组中,判断是否在这个数组中就行了。<br><br>不需要再去其他接口查询地址,然后根据地址或运营商的名字来判断,反而麻烦。<br>PS:这个数组不大,不到800条。<br><br>换算的方法请看此页面:http://url.cn/0TCFrY  内附全中国可划分子网的excel下载<br><br>
<br><font color="#e78608">------解决方案--------------------</font><br>ftp://ftp.apnic.net/public/apnic/stats/apnic/delegated-apnic-latest<br><br>apnic|CN|ipv4|180.212.0.0|131072|20091001|allocated<br><br>这就是国内的. 分析提取. 然后过滤IP就可以了 <div class="clear">
                 
              
              
        
            </div>
Copy after login
Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!