Thư viện GeoLite2-City.mmdb có thể cập nhật mới trên trang
Thư viện geoip2.phar có thể cập nhật mới trên trang
Sau đây là ví dụ đã hoàn thành :)
Ví dụ 1: Dùng thư viện
Link:
<?php
require_once("geoip2.phar");
use GeoIp2\Database\Reader;
// City DB
$reader = new Reader('GeoLite2-City.mmdb');
echo '<pre>';
var_export("123.20.170.47");
echo '</pre>';
$record = $reader->city("123.20.170.47");
// 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");
Ví dụ 2: Dùng luôn một ví dụ có sẵn :)
C:\xampp\htdocs\scss\test.php
<?php
require_once 'vendor/autoload.php';
use MaxMind\Db\Reader;
$ipAddress = '123.20.170.47';
$databaseFile = 'GeoIP2-City.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.
echo '<pre>';
var_export($reader->getWithPrefixLen($ipAddress));
echo '</pre>';
$reader->close();
Vì 2 file GeoIP2-City.mmdb, GeoIP2-Country.mmdb có dữ liêu lớn không cho phép tải ở đây vì vậy phải tải lên google drive :)))