Software77 is perhaps the first company which created IP to country database. On their website you can grab the DB in CSV and GeoIP format with a few example scripts in various languages to use the DB. I couldn’t find anything in PHP to operate on their GeoIP format DB, so I converted the CSV version to PHP arrays and created a simple script to handle this new format.
$i = new Ip2Country; //run below function once only. It will parse IpToCountry.csv //file into PHP files and save them into php_db directory $i->parseCSV(); //to display countryCode: echo $i->load('184.108.40.206')->countryCode; //to display country and countryCode: $i->load('220.127.116.11'); echo $i->countryCode; echo $i->country;
Download link to the DB in CSV format:
PHP class download:
Function parseCSV() is storing over 99 000 entries in a memory table before saving to files. Each table entry in PHP is taking a lot of space, so the script needs more than 40 MB of RAM. So I added the parseCSV2() function, which does exactly the same, but requires less than 1 MB of RAM. New function is a bit slower, as it is doing more disk operations.
Fixed a bug: IPs are now compared as floats not strings. Thanks EdwardRF for spotting the bug!