Home  >  Article  >  Backend Development  >  php GeoIP usage tutorial_PHP tutorial

php GeoIP usage tutorial_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:31:10863browse

GeoIP introduction:
What is GepIP?
The so-called GeoIP is to use the visitor's IP to locate his longitude and latitude, country/region, province, city, and even street location information. The technology here is not a difficult problem, the key lies in having an accurate database. With accurate data sources, you can earn a little money with rare goods, but what we pursue is to promote the spirit of cooperation and collective contribution for everyone to enjoy.
How to use GeoIP?
First we need data information, so first obtain a free database: GeoIP.dat.gz, then decompress it to get: GeoIP.dat, and then operate the data file on demand. The example here uses PHP.
Using GeoIP + PHP
Method 1:
Download GeoIP’s PHP file geoip.inc. Package download

Copy code The code is as follows:

include("geoip.inc.php");
// Open data file
$gi = geoip_open("GeoIP.dat",GEOIP_STANDARD);
// Get country code
$country_code = geoip_country_code_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country code is: $country_code ";
// Get the country name
$country_name = geoip_country_name_by_addr($gi, $_SERVER['REMOTE_ADDR']);
echo "Your country name is: $country_name ";
// Close the file
geoip_close($gi);

Note: If you test locally, $_SERVER['REMOTE_ADDR'] and $_SERVER['REMOTE_ADDR' ] may be 127.0.0.1, so the output content is empty. You can bring in the IP test yourself

Method 2:
Install GeoIP as a PHP extension
yum install GeoIP GeoIP-data GeoIP-devel

Download the GeoIP database
wget http ://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
gzip -d GeoLiteCity.dat.gz
mv GeoLiteCity.dat /var/lib/GeoIP/GeoIPCity.dat

Download the PECL extension for GeoIP
Download address http://pecl.php.net/package/geoip
wget -c http://pecl.php.net/get/geoip-1.0.7 .tgz
tar -zxvf geoip-1.0.7.tgz

Install PECL extension for GeoIP
cd geoip-1.0.7
/usr/local/php/bin/phpize
./configure --with-php-config=/usr/local/php/bin/php-config --with-geoip
make
make install

Add in php.ini
extension=geoip.so
Then restart php and that’s it
Now, you can use some of the GeoIP functions in the php manual

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/323073.htmlTechArticleGeoIP introduction: What is GepIP? The so-called GeoIP is to locate the visitor's latitude and longitude, country/ Location information such as region, province, city, or even street. The technology here is not difficult...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn