PHP uses GeoIP library instance _php instance

Source: Internet
Author: User
Tags geoip geoip database maxmind

It should be noted that Maxmind is the provider of the GeoIP database,
It 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.
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 range of. Inc and. PHP is not dependent on the environment, as long as the support of PHP, direct require can be used.

One, GeoIP database

Http://dev.maxmind.com/geoip/geolite
Subdivision to Country: Geolite Country
Subdivided into cities: Geolite City

Second, the official PHP Geoip.dll Library

Download DLL http://windows.php.net/downloads/pecl/releases/geoip/1.0.8/
Modify php.ini to enable GEOIP libraries:

Copy Code code as follows:
Extension=php_geoip.dll

Append GEOIP segment, specify database location:
Copy Code code as follows:
[GeoIP]
Geoip.custom_directory = "D:\php5.3\geo\"

Test code

Pay attention to the GeoIPCity.dat that is used in Geoip.dll, that is, GeoLiteCity.dat.

Copy Code code 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"));

Third, 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
The same directory uses "./geoip.dat" or "./geolitecity.dat".
Detailed to the country

Copy Code code 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);

Detailed to the National city

Copy Code code 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->longitude. "\ 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->longitude. "\ n";
Print $record->metro_code. "\ n";
Print $record->area_code. "\ n";
Print $record->continent_code. "\ n";
Geoip_close ($GI);

Look at your own development environment and specific circumstances decide what kind of

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.