A small tool to check What is my IP address?. Plus some MaxMind GeoIP data like city, country or longitude and latitude of an IP.
Your IP is:
The following form shows the geographical location of an IP.
Here just a short explaination of the PHP code behind that tool.
A very common place for the client IP address is the
server variable
REMOTE_ADDR
.
$ip = $_SERVER['REMOTE_ADDR'];
I would recommend to validate the IP via filter_var.
if (filter_var($ip, FILTER_VALIDATE_IP)) {
// $ip is an IP
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
// $ip is an IPV4
}
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV6)) {
// $ip is an IPV6
}
}
Install maxmind-db/reader via Composer.
composer require maxmind-db/reader
Using version ^1.1 for maxmind-db/reader
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
Package operations: 1 install, 0 updates, 0 removals
- Installing maxmind-db/reader (v1.1.3): Cloning 7eeccf61b0
Writing lock file
Generating autoload files
You can download free GeoIP databases at MaxMind.
A minimal exmaple of the MaxMind database.
$dbFilePath = '/path_to/GeoLite2-City.mmdb';
$maxMindReader = new MaxMind\Db\Reader($dbFilePath);
print_r($maxMindReader->get($ip));
With the following result.
Array
(
[city] => Array
(
[geoname_id] => 2950159
[names] => Array
(
[de] => Berlin
[en] => Berlin
[es] => Berlín
[fr] => Berlin
[ja] => ベルリン
[pt-BR] => Berlim
[ru] => Берлин
[zh-CN] => 柏林
)
)
[continent] => Array
(
[code] => EU
[geoname_id] => 6255148
[names] => Array
(
[de] => Europa
[en] => Europe
[es] => Europa
[fr] => Europe
[ja] => ヨーロッパ
[pt-BR] => Europa
[ru] => Европа
[zh-CN] => 欧洲
)
)
[country] => Array
(
[geoname_id] => 2921044
[iso_code] => DE
[names] => Array
(
[de] => Deutschland
[en] => Germany
[es] => Alemania
[fr] => Allemagne
[ja] => ドイツ連邦共和国
[pt-BR] => Alemanha
[ru] => Германия
[zh-CN] => 德国
)
)
[location] => Array
(
[accuracy_radius] => 20
[latitude] => 52.5167
[longitude] => 13.4
[time_zone] => Europe/Berlin
)
[postal] => Array
(
[code] => 12529
)
[registered_country] => Array
(
[geoname_id] => 2921044
[iso_code] => DE
[names] => Array
(
[de] => Deutschland
[en] => Germany
[es] => Alemania
[fr] => Allemagne
[ja] => ドイツ連邦共和国
[pt-BR] => Alemanha
[ru] => Германия
[zh-CN] => 德国
)
)
[subdivisions] => Array
(
[0] => Array
(
[geoname_id] => 2950157
[iso_code] => BE
[names] => Array
(
[de] => Berlin
[en] => Land Berlin
[es] => Berlín
[fr] => Berlin
[ja] => ベルリン
[pt-BR] => Berlim
[ru] => Берлин
[zh-CN] => 柏林
)
)
)
)