Maxmind GeoIP2 教程(操作方法)?
Maxmind GeoIP2 tutorial (How-to)?
我用的是 GeoIp,纯 PHP 代码..
但是 GeoIp2 变成了命名空间等等,此时我无法找到如何使用它。我已经下载了 GeoLite2-Country.mmdb
,现在如何获取 IP 的国家名称,即 123.123.123.123
.
p.s。我没有 GIT/COMPOSER 等..
我是怎么做到的:比方说,创建一个名为“My_Folder”的文件夹并在其中:
1) 创建文件夹 GeoIp2
并将此 "SRC" 文件夹 (download) 的内容放入其中。
2) 放入 MaxMind
文件夹(download,来自 "SRC" 文件夹)。
3) 地点即 GeoLite2-Country.mmdb
(download).
然后,在 My_Folder 中创建一个 example.php
文件并放入此代码:
$user_ip='123.123.123.123';
spl_autoload_register('func888'); function func888($class){ include_once(str_replace(array('/','\'), DIRECTORY_SEPARATOR, dirname(__file__)."/$class.php")) ;}
use GeoIp2\Database\Reader;
//you can do it for "city" too.. just everywhere change phrase "country" with "city".
try{
$reader = new Reader(dirname(__file__)."/GeoLite2-Country.mmdb");
$record = $reader->country($user_ip);
$reader->close();
$country_name = $record->raw['country']['names']['en'];
} catch ( GeoIp2\Exception\AddressNotFoundException $e ){ $country_name = 'not_found'; }
echo $country_name;
// RESULTS -------------- > China
p.s。其他示例位于:https://github.com/maxmind/GeoIP2-php
我用的是 GeoIp,纯 PHP 代码..
但是 GeoIp2 变成了命名空间等等,此时我无法找到如何使用它。我已经下载了 GeoLite2-Country.mmdb
,现在如何获取 IP 的国家名称,即 123.123.123.123
.
p.s。我没有 GIT/COMPOSER 等..
我是怎么做到的:比方说,创建一个名为“My_Folder”的文件夹并在其中:
1) 创建文件夹 GeoIp2
并将此 "SRC" 文件夹 (download) 的内容放入其中。
2) 放入 MaxMind
文件夹(download,来自 "SRC" 文件夹)。
3) 地点即 GeoLite2-Country.mmdb
(download).
然后,在 My_Folder 中创建一个 example.php
文件并放入此代码:
$user_ip='123.123.123.123';
spl_autoload_register('func888'); function func888($class){ include_once(str_replace(array('/','\'), DIRECTORY_SEPARATOR, dirname(__file__)."/$class.php")) ;}
use GeoIp2\Database\Reader;
//you can do it for "city" too.. just everywhere change phrase "country" with "city".
try{
$reader = new Reader(dirname(__file__)."/GeoLite2-Country.mmdb");
$record = $reader->country($user_ip);
$reader->close();
$country_name = $record->raw['country']['names']['en'];
} catch ( GeoIp2\Exception\AddressNotFoundException $e ){ $country_name = 'not_found'; }
echo $country_name;
// RESULTS -------------- > China
p.s。其他示例位于:https://github.com/maxmind/GeoIP2-php