Php GeoIP library usage tutorial 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
- 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 GeoIP's PECL extension 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 to php. ini and restart php. Then, you can use some GeoIP functions in the php Manual. |