Is there an IP range for a certain country?

advertisements

We are in a business where we need to block visitors from certain areas or countries. We want to show 403 error page when visitors comes from that certain areas.

Now what we can do is, on every request, get the visitors IP address and get the country name for that IP using any third-party services like Telize or ipapi.co and if it from that country, stop and show the error page.

But the problem is, it will check for all others visitors and if we do a curl on every request, it will definitely slow down our website.

Is there any way we can get the country name from IP address without using any third-party service or curl request or anything that will not slow down our website?

We are using PHP & Symfony 3 framework on a VPS, and speed and performance are very important for us, in case it helps you.

At this moment we want to block visitors from Cameroon, is there any range of IP is assigned for Cameroon?


You can use the Maxmind GeoIP library for php.

The idea is that you download a database (which is just a file) containing geographical information for all the IPs in the world. Since the database is on your server, and you call it using the library, it won't slow down your server. Actually, getting the country code from an IP is so fast the performance impact will be negligible.

The database is updated regularly, so you can periodically re-download it to stay up-to-date. You can get details about the downloadable databases here.