PHP Application GeoIP Library Example Tutorial

Source: Internet
Author: User

PHP Application GeoIP Library Example Tutorial

Note that Maxmind is the provider of the GEOIP database and also provides sample and API documentation for many languages. For example, PHP, and PHP GeoIP library is very different, including the use of the interface function. PHP official GeoIP needs to configure the PHP environment, load the Geoip.dll library, and specify the GeoIP library address in php.ini. Maxmind offers a range of. Inc and PHP for the environment, so long as PHP is supported, it can be used directly require. (PHP GeoIP Library Starter Instance)

I. GEOIP database Http://dev.maxmind.com/geoip/geolite subdivision to Country: GeoLite Country subdivision to city: GeoLite Urban

Second, PHP official Geoip.dll library Download DLL http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/modify php.ini, enable the GeoIP library:

    1. Extension=php_geoip.dll
Copy Code

Append GEOIP segment, specify database location:

    1. [GeoIP]
    2. Geoip.custom_directory = "D:\php5.3\geo\"
Copy Code

The test code notes that the GeoIPCity.dat that are used in Geoip.dll is GeoLiteCity.dat, and watch for hints when used.

    1. Echo geoip_country_name_by_name ("8.8.8.8"). "\ n";
    2. Print_r (Geoip_record_by_name ("8.8.8.8"));
    3. Echo geoip_country_name_by_name ("61.139.2.69"). "\ n";
    4. Print_r (Geoip_record_by_name ("61.139.2.69"));
Copy Code

Iii. Maxmind official PHP File Library documentation and examples: Http://dev.maxmind.com/geoip/downloadable Modify Maxmind example in sample.php and sample_city.php The Geoip.dat/geolitecity.dat path is the same directory as your own path with "./geoip.dat" or "./geolitecity.dat".

1, detailed to the country

    1. Include ("Geoip.inc");
    2. $gi = Geoip_open ("./geoip.dat", Geoip_standard);
    3. Echo geoip_country_code_by_addr ($gi, "8.8.8.8"). "\ T". GEOIP_COUNTRY_NAME_BY_ADDR ($gi, "8.8.8.8"). "\ n";
    4. Echo geoip_country_code_by_addr ($gi, "61.139.2.69"). "\ T". GEOIP_COUNTRY_NAME_BY_ADDR ($gi, "61.139.2.69"). "\ n";
    5. Geoip_close ($GI);
Copy Code

2, detailed to the National city

  1. Include ("Geoipcity.inc");
  2. Include ("geoipregionvars.php");
  3. $gi = Geoip_open ("./geolitecity.dat", Geoip_standard);
  4. $record = Geoip_record_by_addr ($gi, "8.8.8.8");
  5. Print $record->country_code. " " . $record->country_code3. " " . $record->country_name. "\ n";
  6. Print $record->region. " " . $GEOIP _region_name[$record->country_code][$record->region]. "\ n";
  7. Print $record->city. "\ n";
  8. Print $record->postal_code. "\ n";
  9. Print $record->latitude. "\ n";
  10. Print $record->longitude. "\ n";
  11. Print $record->metro_code. "\ n";
  12. Print $record->area_code. "\ n";
  13. Print $record->continent_code. "\ n";
  14. print "\ n-----\ n";
  15. $record = Geoip_record_by_addr ($gi, "61.139.2.69");
  16. Print $record->country_code. " " . $record->country_code3. " " . $record->country_name. "\ n";
  17. Print $record->region. " " . $GEOIP _region_name[$record->country_code][$record->region]. "\ n";
  18. Print $record->city. "\ n";
  19. Print $record->postal_code. "\ n";
  20. Print $record->latitude. "\ n";
  21. Print $record->longitude. "\ n";
  22. Print $record->metro_code. "\ n";
  23. Print $record->area_code. "\ n";
  24. Print $record->continent_code. "\ n";
  25. Geoip_close ($GI);
Copy Code

When using the above code, you can decide which to use depending on the development environment and the specific circumstances.

  • 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.