How create GeoIP functionality in PHP project?

2019-03-31 10:04发布

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.

5条回答
在下西门庆
2楼-- · 2019-03-31 10:32

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

$details = geoip_record_by_name($_SERVER['REMOTE_ADDR']);
echo $details['city'];
查看更多
家丑人穷心不美
3楼-- · 2019-03-31 10:32

I decided to use the free database from http://lite.ip2location.com/ Works better than all the others did so far.

查看更多
男人必须洒脱
4楼-- · 2019-03-31 10:39

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

查看更多
唯我独甜
5楼-- · 2019-03-31 10:48

Also you can have a look here: MaxMind GeoIP PHP API

查看更多
爷的心禁止访问
6楼-- · 2019-03-31 10:50

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.

查看更多
登录 后发表回答