Php uses GeoIP Library instance _ php instance

Source: Internet
Author: User
Tags geoip geoip database maxmind
This article mainly introduces the use of GeoIP Library instances in php. This article provides code instances subdivided into countries and cities, for more information, see maxmind, which is the provider of the geoip database,
Sample and api instructions in many languages are also provided.
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.

1. GEOIP database

Http://dev.maxmind.com/geoip/geolite
Sub-Country: GeoLite Country
Sub-City: GeoLite City

II. Official php geoip. dll Library

Download dll http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/
Modify php. ini to enable the geoip Library:

The code is as follows:

Extension = php_geoip.dll


Append the geoip segment and specify the database location:

The code is as follows:

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

Test Code

Note that GeoIPCity. dat used in geoip. dll is GeoLiteCity. dat. when using it, pay attention to the prompt.

The code is as follows:


Echo geoip_country_name_by_name ("8.8.8.8"). "\ n ";
Print_r (geoip_record_by_name ("8.8.8.8 "));
Echo geoip_country_name_by_name ("61.139.2.69"). "\ n ";
Print_r (geoip_record_by_name ("61.139.2.69 "));

3. MaxMind official PHP file function library

Documents and examples: http://dev.maxmind.com/geoip/downloadable
Modify the GeoIP. dat/GeoLiteCity. dat path in sample. php and sample_city.php in the maxmind example to your own path.
Use "./GeoIP. dat" or "./GeoLiteCity. dat" in the same directory.
Detailed to country

The code is as follows:


Include ("geoip. inc ");
$ Gi = geoip_open ("./GeoIP. dat", GEOIP_STANDARD );
Echo geoip_country_code_by_addr ($ gi, "8.8.8.8"). "\ t". geoip_country_name_by_addr ($ gi, "8.8.8.8"). "\ n ";
Echo geoip_country_code_by_addr ($ gi, "61.139.2.69"). "\ t". geoip_country_name_by_addr ($ gi, "61.139.2.69"). "\ n ";
Geoip_close ($ gi );

Detail to country cities

The code is as follows:


Include ("geoipcity. inc ");
Include ("geoipregionvars. php ");
$ Gi = geoip_open ("./GeoLiteCity. dat", GEOIP_STANDARD );

$ Record = geoip_record_by_addr ($ gi, "8.8.8.8 ");
Print $ record-> country_code. "". $ record-> country_code3. "". $ record-> country_name. "\ n ";
Print $ record-> region. "". $ GEOIP_REGION_NAME [$ record-> country_code] [$ record-> region]. "\ n ";
Print $ record-> city. "\ n ";
Print $ record-> postal_code. "\ n ";
Print $ record-> latitude. "\ n ";
Print $ record-> longpolling. "\ n ";
Print $ record-> metro_code. "\ n ";
Print $ record-> area_code. "\ n ";
Print $ record-> continent_code. "\ n ";

Print "\ n ----- \ n ";
$ Record = geoip_record_by_addr ($ gi, "61.139.2.69 ");
Print $ record-> country_code. "". $ record-> country_code3. "". $ record-> country_name. "\ n ";
Print $ record-> region. "". $ GEOIP_REGION_NAME [$ record-> country_code] [$ record-> region]. "\ n ";
Print $ record-> city. "\ n ";
Print $ record-> postal_code. "\ n ";
Print $ record-> latitude. "\ n ";
Print $ record-> longpolling. "\ n ";
Print $ record-> metro_code. "\ n ";
Print $ record-> area_code. "\ n ";
Print $ record-> continent_code. "\ n ";
Geoip_close ($ gi );

Depends on your development environment and the specific situation to decide which one to use

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.