略微加速

PHP官方手册 - 互联网笔记

PHP - Manual: GeoIP

2024-11-22

Geo IP 定位

add a noteadd a note

User Contributed Notes 5 notes

up
29
webmaster at isag dot melbourne
3 years ago
With GeoIP2, the easiest way is to:

* Grab the latest GeoIP2 Lite Database(s): https://dev.maxmind.com/geoip/geoip2/geolite2/
* Grab the latest geoip2.phar: https://github.com/maxmind/GeoIP2-php/releases

<?php
require_once("geoip2.phar");
use
GeoIp2\Database\Reader;
// City DB
$reader = new Reader('/path/to/GeoLite2-City.mmdb');
$record = $reader->city($_SERVER['REMOTE_ADDR']);
// or for Country DB
// $reader = new Reader('/path/to/GeoLite2-Country.mmdb');
// $record = $reader->country($_SERVER['REMOTE_ADDR']);
print($record->country->isoCode . "\n");
print(
$record->country->name . "\n");
print(
$record->country->names['zh-CN'] . "\n");
print(
$record->mostSpecificSubdivision->name . "\n");
print(
$record->mostSpecificSubdivision->isoCode . "\n");
print(
$record->city->name . "\n");
print(
$record->postal->code . "\n");
print(
$record->location->latitude . "\n");
print(
$record->location->longitude . "\n");
$>
up
30
mark at moderndeveloperllc dot com
8 years ago
It should be noted that this extension has now been superseded by the GeoIP2 API that MaxMind now produces. There is a pure-PHP set of classes and a C library and extension you can optionally install. The code can be found in various projects on MaxMind's GitHub page: https://github.com/maxmind/
up
1
ruben dot benjamin at hidden-email dot com
4 months ago
If you want to use the C Library
Example for Ubuntu and PHP 7.4

sudo apt install libmaxminddb0 libmaxminddb-dev mmdb-bin
pecl install maxminddb
vi /etc/php/7.4/mods-available/maxmind.ini
add
extension=maxminddb.so
phpenmod maxmind

In code
<?php

use MaxMind\Db\Reader;

$ipAddress = '24.24.24.24';
$databaseFile = '/usr/share/GeoIP/GeoLite2-Country.mmdb';

$reader = new Reader($databaseFile);

// get returns just the record for the IP address
print_r($reader->get($ipAddress));

// getWithPrefixLen returns an array containing the record and the
// associated prefix length for that record.
print_r($reader->getWithPrefixLen($ipAddress));

$reader->close();
up
-4
SG
2 years ago
The 3rd party geolite2legacy script can also used to convert the newer GeoLite2 format downloads into the legacy format which can be read by the PHP GeoIP extension.
up
-47
istsehrgut
7 years ago
If you still want to use legacy binaries but also need IPv6 support this should help:

In order to support IPv6->Country code easily and without unnecessary files based on the integration above:

Grab a copy of the latest legacy IPv6 data (I'm assuming you already have IPv4 binary):

wget http://geolite.maxmind.com/download/geoip/database/GeoIPv6.dat.gz
Decompress and move it to a dir accessible to your web server:

gunzip GeoIPv6.dat
mv GeoIPv6.dat /etc/usr/share/GeoIP/GeoIPv6.dat
Grab a copy of geoip.inc from the Maxmind git dir (https://github.com/maxmind/geoip-api-php/blob/master/src/geoip.inc) and save it somewhere you can access wherever you'll need to run geoip.

If you have php5-geoip installed as I did, remove it with sudo apt-get remove php5-geoip; purge as necessary.

With the above done you can now test incoming IP address for v4 or v6 and get appropriate results.

Example:

<?php
include_once('geoip.inc');

//set an IPv6 address for testing
$ip='2601:8:be00:cf20:ca60:ff:fe09:35b5';

/*
test if $ip is v4 or v6 and assign appropriate .dat file in $gi
run appropriate function geoip_country_code_by_addr() vs geoip_country_code_by_addr_v6()  
*/
if((strpos($ip, ":") === false)) {
   
//ipv4
   
$gi = geoip_open("/usr/share/GeoIP/GeoIP1.dat",GEOIP_STANDARD);
   
$country = geoip_country_code_by_addr($gi, $ip);
}
else {
   
//ipv6
   
$gi = geoip_open("/usr/share/GeoIP/GeoIPv6.dat",GEOIP_STANDARD);
   
$country = geoip_country_code_by_addr_v6($gi, $ip);
}
echo
$ip . "<br>" . $country;
This is specifically for Country, but can easily be replicated for City data.

官方地址:https://www.php.net/manual/en/book.geoip.php

北京半月雨文化科技有限公司.版权所有 京ICP备12026184号-3