Block domestic IP, Access requires authentication
There is a foreign trade site want to block the domestic IP, but we need to manage and allow specific domestic users to access so to be authenticated, how can such a function be implemented? First post, please pay more attention! Thank you!
------Solution--------------------
Can make a page, when the user visits,
1. Get the IP of the user
2. Then go to the open IP interface to query the user's geographical location (such as Sina, NetEase, ip138, there are open IP query interface)
3. With the user's geographical location, then decide whether to block it, or verify the
------Solution--------------------
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 ("/Power/", $str) | |!preg_match ("/Pass/", $STR))? $location [' sp '] = $tmp [7]: $location [' sp '] = $tmp [6]; $location [' time '] = Microtime (TRUE)-$php _runtime; if (!empty ($location)) return $location; else return false;}
------Solution--------------------
The IP address that the user visited can be used to figure out which subnet the IP belongs to, such as 110.80.0.0/13
The entire China can be divided into an array of subnets, to determine whether in this array is OK.
Do not need to go to other interfaces to query the address, and then according to the address or operator's name to judge, but trouble.
PS: This array is small, less than 800.
The method of conversion please see this page: Http://url.cn/0TCFrY with all the Chinese can be divided into sub-network Excel download
------Solution--------------------
Ftp://ftp.apnic.net/public/apnic/stats/apnic/delegated-apnic-latest
apnic| cn|ipv4|180.212.0.0|131072|20091001|allocated
This is the domestic. Analysis extraction. And then filter the IP to do it.