Php GeoIP usage tutorial

Source: Internet
Author: User
Tags geoip geoip database

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, 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
Gzip-d GeoLiteCity.dat.gz
Mv GeoLiteCity. dat/var/lib/GeoIP/GeoIPCity. dat

Download the PECL extension of GeoIP
Http:// wget-c
Tar-zxvf geoip-1.0.7.tgz

Install PECL extension of GeoIP
Cd geoip-1.0.7
./Configure -- with-php-config =/usr/local/php/bin/php-config -- with-geoip
Make install

Extension = geoip. so
Restart php.
Now, 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: 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.