89 lines
3.4 KiB
PHP
89 lines
3.4 KiB
PHP
<?php
|
|
ini_set('display_errors', 1);
|
|
ini_set('display_startup_errors', 1);
|
|
|
|
require_once '../geoip/vendor/autoload.php';
|
|
use GeoIp2\Database\Reader;
|
|
|
|
// This creates the Reader object, which should be reused across
|
|
// lookups.
|
|
$reader = new Reader('../geoip/GeoIP2-City.mmdb');
|
|
|
|
// Replace "city" with the appropriate method for your database, e.g.,
|
|
// "country".
|
|
$record = $reader->city(get_client_ip());
|
|
print(get_client_ip() . "\n"); // 'US'
|
|
print(getIPAddress() . "\n"); // 'US'
|
|
|
|
print($record->country->isoCode . "\n"); // 'US'
|
|
print($record->country->name . "\n"); // 'United States'
|
|
print($record->country->names['zh-CN'] . "\n"); // '美国'
|
|
|
|
print($record->mostSpecificSubdivision->name . "\n"); // 'Minnesota'
|
|
print($record->mostSpecificSubdivision->isoCode . "\n"); // 'MN'
|
|
|
|
print($record->city->name . "\n"); // 'Minneapolis'
|
|
|
|
print($record->postal->code . "\n"); // '55455'
|
|
|
|
print($record->location->latitude . "\n"); // 44.9733
|
|
print($record->location->longitude . "\n"); // -93.2323
|
|
|
|
function get_client_ip() {
|
|
$ipaddress = '';
|
|
if (isset($_SERVER['HTTP_CLIENT_IP']))
|
|
$ipaddress = $_SERVER['HTTP_CLIENT_IP'];
|
|
else if(isset($_SERVER['HTTP_X_FORWARDED_FOR']))
|
|
$ipaddress = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
|
else if(isset($_SERVER['HTTP_X_FORWARDED']))
|
|
$ipaddress = $_SERVER['HTTP_X_FORWARDED'];
|
|
else if(isset($_SERVER['HTTP_FORWARDED_FOR']))
|
|
$ipaddress = $_SERVER['HTTP_FORWARDED_FOR'];
|
|
else if(isset($_SERVER['HTTP_FORWARDED']))
|
|
$ipaddress = $_SERVER['HTTP_FORWARDED'];
|
|
else if(isset($_SERVER['REMOTE_ADDR']))
|
|
$ipaddress = $_SERVER['REMOTE_ADDR'];
|
|
else
|
|
$ipaddress = 'UNKNOWN';
|
|
return $ipaddress;
|
|
}
|
|
|
|
|
|
function getIPAddress() {
|
|
$ipAddress = '';
|
|
if (! empty($_SERVER['HTTP_CLIENT_IP']) && $this->isValidIpAddress($_SERVER['HTTP_CLIENT_IP'])) {
|
|
// check for shared ISP IP
|
|
$ipAddress = $_SERVER['HTTP_CLIENT_IP'];
|
|
} else if (! empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
|
// check for IPs passing through proxy servers
|
|
// check if multiple IP addresses are set and take the first one
|
|
$ipAddressList = explode(',', $_SERVER['HTTP_X_FORWARDED_FOR']);
|
|
foreach ($ipAddressList as $ip) {
|
|
if (isValidIpAddress($ip)) {
|
|
$ipAddress = $ip;
|
|
break;
|
|
}
|
|
}
|
|
} else if (! empty($_SERVER['HTTP_X_FORWARDED']) && $this->isValidIpAddress($_SERVER['HTTP_X_FORWARDED'])) {
|
|
$ipAddress = $_SERVER['HTTP_X_FORWARDED'];
|
|
} else if (! empty($_SERVER['HTTP_X_CLUSTER_CLIENT_IP']) && $this->isValidIpAddress($_SERVER['HTTP_X_CLUSTER_CLIENT_IP'])) {
|
|
$ipAddress = $_SERVER['HTTP_X_CLUSTER_CLIENT_IP'];
|
|
} else if (! empty($_SERVER['HTTP_FORWARDED_FOR']) && $this->isValidIpAddress($_SERVER['HTTP_FORWARDED_FOR'])) {
|
|
$ipAddress = $_SERVER['HTTP_FORWARDED_FOR'];
|
|
} else if (! empty($_SERVER['HTTP_FORWARDED']) && $this->isValidIpAddress($_SERVER['HTTP_FORWARDED'])) {
|
|
$ipAddress = $_SERVER['HTTP_FORWARDED'];
|
|
} else if (! empty($_SERVER['REMOTE_ADDR']) && $this->isValidIpAddress($_SERVER['REMOTE_ADDR'])) {
|
|
$ipAddress = $_SERVER['REMOTE_ADDR'];
|
|
}
|
|
return $ipAddress;
|
|
}
|
|
|
|
function isValidIpAddress($ip)
|
|
{
|
|
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4 | FILTER_FLAG_IPV6 | FILTER_FLAG_NO_PRIV_RANGE | FILTER_FLAG_NO_RES_RANGE) === false) {
|
|
return false;
|
|
}
|
|
return true;
|
|
}
|
|
|