php uses an GeoIP library instance

  • 2021-07-02 23:49:30
  • OfStack

It should be noted that maxmind is the provider of geoip database,
sample and api documentation is also provided in many languages.
For example, php and php's geoip library are quite different, including usage methods and interface functions.
The official geoip of php needs to configure php environment, load geoip. dll library, and specify GEOIP library address in php. ini.
maxmind provides series 1. inc and. php are environment-independent. As long as php is supported, it can be used directly after require.

1. GEOIP database

http://dev.maxmind.com/geoip/geolite
Subdivided into countries: GeoLite Country
Subdivided into cities: GeoLite City

2. php Official geoip. dll Library

Download dll http://windows.php.net/downloads/pecl/releases/geoip/1. 0.8/
Modify php. ini to enable the geoip library:

extension=php_geoip.dll

Append the geoip segment to specify the database location:
[geoip]
geoip.custom_directory = "D:\php5.3\geo\"

Test code

Pay attention to GeoIPCity. dat used in geoip. dll, that is, GeoLiteCity. dat, and pay attention to the tips when using it


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

Documentation and examples: http://dev. maxmind. com/geoip/downloadable
Modify the GeoIP. dat/GeoLiteCity. dat path in maxmind example sample. php and sample_city. php to your own path
Use "./GeoIP. dat" or "./GeoLiteCity. dat" for the same directory.
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);

Detailed to national cities


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

Depending on your own development environment and specific circumstances, decide which one to use


Related articles: