![]() |
| How do I restrict traffic based on country of origin? |
|
jamie
HostMySite Sales Rep
![]()
|
In some cases you want to prevent visitors from your site if they aren't within the US; below is a technique I used to redirect traffic from an application that only allowed American users. Note that though I'm doing this in the application layer, you can also enact similar restrictions using IP-based firewall rules on a hardware or software firewall (if software firewall then Windows Firewall or IPTables in Linux should fit the bill). Also, I know that IIS has the ability to block traffic from certain IPs and IP ranges as well, so you may want to implement it there instead.
If you'd rather do your blocking using code though, an example can be found here: http://www.phpjabbers.com/redirect-based-on-referrer-or-ip-address-php2.html This is a little better in some respects because you can direct anyone who's been blocked to an error page that gives them an explanation plus a form they can fill out if they think they should be exempt. This was useful to me because in my app I had a US Marine get blocked while they were overseas, so I was able to code in an exception for their IP address. The nice part about it was the redirect page I used pulled and displayed the user's IP address so they could tell me what it was - something a typical internet user won't know. The important part there is the $_SERVER['REMOTE_ADDR'] value, since that gives you the IP of the visitor. In the code example there you’re running a match vs a single IP address; what you’d want to do instead if comparing based on class A addresses is store all of the ‘good’ IP ranges in your database then do a comparison based on that. So, 1. Create a db table containing the first octet of the ARIN ranges (i.e. it would have one field, IP, and would contain values like 63 64 65 66 67 And so on. Note these values stand for the first octect; the full range would be read as 63.0.0.0 64.0.0.0 65.0.0.0 and so on, but since you don’t need the full number there is no need to store it as a full IP. 2. In the code above you would modify it to read in the $_SERVER['REMOTE_ADDR'] and then manipulate that string to only pull the first octet. So, if a visitor’s IP address was 78.59.12.53 You would convert that to just 78 then store it as a variable. 3. Finally, run a dbcheck on the variable you just saved vs the values of your ‘good ip’ table to see if 78 is found and redirect traffic based on that. Keep in mind you could do this ‘backwards’ as well and store ‘bad ips’ in your database instead – looking over the list of assignments I’m not sure which would take up less space. The list of IP assignments can be found here: http://www.iana.org/assignments/ipv4-address-space Anything that shows a WHOIS from ‘whois.arin.net’ is part of the ARIN block, which is administers to North America (i.e. the US and Canada). This link may also be of use to you: http://ip-to-country.webhosting.info/ Note that ARIN also technically administers some of the legacy ranges as well – you can see those in the IANA link I gave – and they belong to companies such ase General Electric, HP, Xerox, etc. Those IPs are not country-specific by class A allocation, so you can’t really tell where they are with this approach, though it’s a good bet most if not all are US-based. |
||||||||||||
|
|
|||||||||||||
|
Buzz Net Marketing
|
Wouldn't it just be easier to read the browser locale setting, instead of doing an ip search against a database, and creating an unnecessarily database query, or even store the allowed, or denied ip blocks in a flat file?
|
||||||||||||
|
|
|||||||||||||
|
whitesites
Forum Regular
|
That would work if the request was coming from a browser. normally the IPs your trying to block are making server based requests. They are a bot, or spider, that is scraping content / emails / photos from your sites. They eat up alot of bandwidth, and worst of all, they don't click my google ads. Plus even if they did google won't pay you for foreign traffic clicks.
I used to block entire IP ranges, but now I use Project Honey Pot or StopForumSpam to stop them. Read my blog for more details http://blog.whitesites.com/asp-net-http-module-for-project-honey-pot-http-IP-blacklist__633426018146370531_blog.htm |
||||||||||||
|
|
|||||||||||||
|
nathacof
Forum Admin
|
MaxMind provides a GeoIP database for free for those of you who want to test drive an enterprise solution, without the price tag:
http://www.maxmind.com/app/geolitecountry http://www.maxmind.com/app/geolitecity http://www.maxmind.com/app/api
|
|||||||||||||
|
|
||||||||||||||
| How do I restrict traffic based on country of origin? |
|
||
|



