Php application GeoIP Library instance tutorial

Source: Internet
Author: User
Tags geoip geoip database maxmind
Php application GeoIP Library instance tutorial

Php application GeoIP Library instance tutorial

Note that maxmind is the provider of the geoip database and provides sample and api instructions in many languages. For example, php and php's geoip library are quite different, including usage methods and interface functions. The official php geoip needs to configure the php environment, load the geoip. dll library, and specify the GEOIP Library address in php. ini. Maxmind provides a series of. inc and. php environments that are not dependent on the environment. you only need to support php and directly use require. (Php GeoIP database entry instance)

1. GEOIP database: http://dev.maxmind.com/geoip/geoliteto Country: GeoLite Country to City: GeoLite City

2. download the dll from the official php geoip. dll Library http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/modify php.ini and enable the geoiplibrary:

  1. Extension = php_geoip.dll

Append the geoip segment and specify the database location:

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

Test code. Note that GeoIPCity. dat used in geoip. dll is GeoLiteCity. dat. Note the prompt when using it.

  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 "));

3. documents and examples of the MaxMind official PHP file function library: http://dev.maxmind.com/geoip/downloadablemodify the sample in maxmind. geoIP in php and sample_city.php. dat/GeoLiteCity. the dat path uses ". /GeoIP. dat "or". /GeoLiteCity. dat.

1. detailed to 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 );

2. go to the country city in detail

  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-> longpolling. "\ 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-> longpolling. "\ n ";
  22. Print $ record-> metro_code. "\ n ";
  23. Print $ record-> area_code. "\ n ";
  24. Print $ record-> continent_code. "\ n ";
  25. Geoip_close ($ gi );

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

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.