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:
- Extension=php_geoip.dll
Copy CodeAppend GEOIP segment, specify database location:
- [GeoIP]
- Geoip.custom_directory = "D:\php5.3\geo\"
Copy CodeThe test code notes that the GeoIPCity.dat that are used in Geoip.dll is GeoLiteCity.dat, and watch for hints when used.
- 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"));
Copy CodeIii. 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
- 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);
Copy Code2, detailed to the National city
- 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);
Copy CodeWhen using the above code, you can decide which to use depending on the development environment and the specific circumstances. |