What is the best and quickest way to block / allow only ranges IPS by CIDR (lock / permission ranges IPs of a country)

StackOverflow https://stackoverflow.com/questions/21163145

  •  28-09-2022
  •  | 
  •  

Question

I need to give access permission to users only if they are from a certain country, I currently do this with PHP, I put all the IP ranges of the country in an array, use one check_cidr function to check if the IP is allowed or not, I also have the method of Allow / Deny in htaccess.

What is the fastest and optimized weight and way less cpu load on the server? There are some official and updated list of all the IP ranges of the countries?

Was it helpful?

Solution

Using the Allow and Deny is probably faster because it doesn't require running a script. The htaccess file will need to be read but as long as it's unchanged, nothing else needs to happen. It would work even faster if you put them in the server/vhost config instead of an htaccess file.

As for allocation blocks, there's several places that you can google:

OTHER TIPS

I would recommend using the PHP Extension API if you are concerned at all about performance. You can get upwards of 7 million queries per second with the PHP (C API) extension.

I describe how to compile the extension, and how to use the mmdb databases in PHP to allow or restrict countries by iso-3316 2-character codes here:

Intro to Maxmind GeoLite2 with Kohana PHP

Geolocate IP Address

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top