PHP GeoIP Library Use tutorial Example

Source: Internet
Author: User

PHP GeoIP Library Usage Tutorials

What is GEPIP? The so-called GeoIP, is through the visitor's IP, positioning his latitude and longitude, country, provinces and cities, and even the location of the street information. The technology is not a problem, the key is to have a precise database. With accurate data sources on the enjoy high earn a little money, but to develop a spirit of cooperation, collective contribution to the enjoyment is our pursuit.

How is GeoIP used? First we need data information, so first get a free database: GeoIP.dat.gz, then extract: GeoIP.dat, then the data file on-demand operation, this example uses PHP. GeoIP + PHP Usage

Method One: Download GeoIP PHP file geoip.inc. Package download

    1. Include ("geoip.inc.php");
    2. Open Data File
    3. $gi = Geoip_open ("GeoIP.dat", Geoip_standard);
    4. Get Country Code
    5. $country _code = geoip_country_code_by_addr ($gi, $_server[' remote_addr ']);
    6. echo "Your Country code is: $country _code";
    7. Get Country Name
    8. $country _name = geoip_country_name_by_addr ($gi, $_server[' remote_addr ']);
    9. echo "Your country name is: $country _name";
    10. Close File
    11. Geoip_close ($GI);
Copy Code

Note: If you test locally because $_server[' remote_addr ' and $_server[' REMOTE_ADDR '] may be 127.0.0.1, the output content is empty. Can bring in the IP test yourself

Method Two: Install GeoIP as PHP extension

    1. Yum Install GeoIP Geoip-data geoip-devel
Copy Code

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
Copy Code

Download GeoIP PECL extension Download address Http://pecl.php.net/package/geoip

    1. Wget-c http://pecl.php.net/get/geoip-1.0.7.tgz
    2. TAR-ZXVF geoip-1.0.7.tgz
Copy Code

Installing the GeoIP PECL extension

    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
Copy Code

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

You can then use the GeoIP partial function 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.