PhpGeoIP library usage tutorial example

Source: Internet
Author: User
PhpGeoIP library usage tutorial example

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

  1. Include ("geoip. inc. php ");
  2. // Open the data file
  3. $ Gi = geoip_open ("GeoIP. dat", GEOIP_STANDARD );
  4. // Obtain country code
  5. $ Country_code = geoip_country_code_by_addr ($ gi, $ _ SERVER ['remote _ ADDR ']);
  6. Echo "Your country code is: $ country_code ";
  7. // Obtain the country name
  8. $ Country_name = geoip_country_name_by_addr ($ gi, $ _ SERVER ['remote _ ADDR ']);
  9. Echo "Your country name is: $ country_name ";
  10. // Close the file
  11. 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

  1. Yum install GeoIP-data GeoIP-devel

Download GeoIP database

  1. Wget http://geolite.maxmind.com/download/geoip/database/GeoLiteCity.dat.gz
  2. Gzip-d GeoLiteCity.dat.gz
  3. Mv GeoLiteCity. dat/var/lib/GeoIP/GeoIPCity. dat

Download GeoIP's PECL extension http://pecl.php.net/package/geoip

  1. Http://pecl.php.net/get/geoip-1.0.7.tgz wget-c
  2. Tar-zxvf geoip-1.0.7.tgz

Install PECL extension of GeoIP

  1. Cd geoip-1.0.7
  2. /Usr/local/php/bin/phpize
  3. ./Configure -- with-php-config =/usr/local/php/bin/php-config -- with-geoip
  4. Make
  5. Make install

Add extension = geoip. so to php. ini and restart php.

Then, you can use some GeoIP functions in the php Manual.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.