GeoIP introduction:
What is GepIP?
GeoIP is used to locate the geographical longitude and latitude of a visitor, country/region, province/city, or even Street. The technology here is not difficult. The key is to have a precise database. With accurate data sources, you can earn a small amount of money for odd goods. However, it is our pursuit to promote the spirit of cooperation and make a collective contribution to everyone's enjoyment.
How to Use GeoIP?
First, we need data information, so we first get a free database: GeoIP.dat.gz, and then decompress it to get: GeoIP. dat, followed by on-demand operations on data files. The example here uses PHP.
Use of GeoIP + PHP
Method 1:
Download the GeoIP PHP file geoip. inc. Package download
Copy codeThe Code is as follows: include ("geoip. inc. php ");
// Open the data file
$ Gi = geoip_open ("GeoIP. dat", GEOIP_STANDARD );
// Obtain Country Code
$ Country_code = geoip_country_code_by_addr ($ gi, $ _ SERVER ['remote _ ADDR ']);
Echo "Your country code is: $ country_code ";
// Obtain 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: For Local testing, the values of $ _ SERVER ['remote _ ADDR '] and $ _ SERVER ['remote _ ADDR'] may be 127.0.0.1, the output content is empty. You can test the IP address on your own.
Method 2:
Install GeoIP into PHP Extension
Yum install GeoIP-data GeoIP-devel
Download 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 of GeoIP
Http://pecl.php.net/package/geoip
Http://pecl.php.net/get/geoip-1.0.7.tgz wget-c
Tar-zxvf geoip-1.0.7.tgz
Install PECL extension of 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
Extension = geoip. so
Restart php.
Now, you can use some GeoIP functions in the php manual.