How create GeoIP functionality in PHP project?

NiLL picture NiLL · Dec 12, 2010 · Viewed 8.8k times · Source

I have some IP adress ($_SERVER['REMOTE_ADDR']) and I must receive (learn) name of country and it would be nice if I can receive (learn) name of city too. And don't forget It's php-project, useful API - very good.

P.S. It's some open-source project and we must use only free and open-source tools.

Answer

crmpicco picture crmpicco · Aug 23, 2012

None (only the 'GeoIP.dat' file is needed). To download a free GeoIP Standard Country database, go to http://maxmind.com/download/geoip/database/

Install

Just place the 'geoip.inc' file somewhere according to the 'include_path' directive of your 'php.ini' file, or just place it in the same directory as your PHP scripts.

Usage

Gets country name by hostname :

include("geoip.inc");

$gi = geoip_open("/usr/local/share/GeoIP/GeoIP.dat",GEOIP_STANDARD);

echo geoip_country_code_by_addr($gi, "24.24.24.24") . "\t" .
     geoip_country_name_by_addr($gi, "24.24.24.24") . "\n";
echo geoip_country_code_by_addr($gi, "80.24.24.24") . "\t" .
     geoip_country_name_by_addr($gi, "80.24.24.24") . "\n";

HTH.