How create GeoIP functionality in PHP project?
Asked Answered
P

3

5

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.

Provenance answered 12/12, 2010 at 17:6 Comment(1)
php.net/manual/en/book.geoip.phpKellogg
P
5

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.

Paulownia answered 23/8, 2012 at 10:36 Comment(2)
Thanks! Helped me a ton, +1. But note you have to geoip_close() and that you'll need the other databases for gathering things like city names and ASNs (their ISPs) and IPv6 support requires the databases suffixed by "v6".Pairoar
This should be best answer . free services like ipinfo.io are not reliable I always I got error message and some time they ban may IP address . Last they use same DB this from their site "This product includes GeoLite2 data "Chilopod
B
2

PHP has some useful builtin GeoIP-functions. They should be sufficient:

$details = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
echo $details['city'];
Baileybailie answered 12/12, 2010 at 17:11 Comment(1)
It's not because they're in the manual that they're built-in, you know... This one isn't.Pairoar
S
0

In Linux Environment 1. sudo yum install php56-devel geoip geoip-devel php-pear 2. sudo pecl install geoip 3. extension=geoip.so (add this line in php.ini) 4. move .dat file in /usr/share/GeoIP folder

In Windows Environment 1. move .dll in ext folder 2. move .dat file in apache/bin folder 3. add dll extension in php.ini

Sweetandsour answered 20/11, 2017 at 9:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.