Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How create GeoIP functionality in PHP project?

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.

like image 552
NiLL Avatar asked Dec 28 '22 04:12

NiLL


1 Answers

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.

like image 125
crmpicco Avatar answered Jan 02 '23 01:01

crmpicco