显示客户端IP的详细信息
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

107 lines
2.9 KiB

<?php
function getIPInfo($ip, $is_cli) {
$specialIpInfo = getSpecialIpInfo($ip);
if (is_string($specialIpInfo)) {
$info['ip'] = $ip;
$info['as'] = null;
$info['city'] = null;
$info['region'] = null;
$info['country'] = null;
$info['timezone'] = null;
$info['loc'] = null;
$info['isp'] = $specialIpInfo;
} else {
$rawIspInfo = getIspInfo($ip);
$info['ip'] = $ip;
$info['as'] = getAS($rawIspInfo);
$info['city'] = $rawIspInfo['city'];
$info['region'] = $rawIspInfo['region'];
$info['country'] = get_country($rawIspInfo['country'])['en'];
$info['timezone'] = $rawIspInfo['timezone'];
$info['loc'] = $rawIspInfo['loc'];
$info['isp'] = getIsp($rawIspInfo);
}
if ($is_cli == true) {
$cli = "IP: ".$info['ip'].PHP_EOL;
$cli = $cli."AS: ".$info['as'].PHP_EOL;
$cli = $cli."City: ".$info['city'].PHP_EOL;
$cli = $cli."Region: ".$info['region'].PHP_EOL;
$cli = $cli."Country: ".$info['country'].PHP_EOL;
$cli = $cli."Timezone: ".$info['timezone'].PHP_EOL;
$cli = $cli."Location: ".$info['loc'].PHP_EOL;
$cli = $cli."ISP: ".$info['isp'].PHP_EOL;
return $cli;
}
return json_encode($info);
}
function getSpecialIpInfo($ip) {
if ('::1' === $ip) {
return 'localhost IPv6 access';
}
if (stripos($ip, 'fe80:') === 0) {
return 'link-local IPv6 access';
}
if (strpos($ip, '127.') === 0) {
return 'localhost IPv4 access';
}
if (strpos($ip, '10.') === 0) {
return 'private IPv4 access';
}
if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) {
return 'private IPv4 access';
}
if (strpos($ip, '192.168.') === 0) {
return 'private IPv4 access';
}
if (strpos($ip, '169.254.') === 0) {
return 'link-local IPv4 access';
}
return null;
}
function getIspInfo($ip) {
$json = file_get_contents('https://ipinfo.io/'.$ip.'/json');
if (!is_string($json)) {
return null;
}
$data = json_decode($json, true);
if (!is_array($data)) {
return null;
}
return $data;
}
function getIsp($rawIspInfo) {
if (
!is_array($rawIspInfo)
|| !array_key_exists('org', $rawIspInfo)
|| !is_string($rawIspInfo['org'])
|| empty($rawIspInfo['org'])
) {
return 'Unknown ISP';
}
return preg_replace('/AS\\d+\\s/', '', $rawIspInfo['org']);
}
function getAS($rawIspInfo) {
if (
!is_array($rawIspInfo)
|| !array_key_exists('org', $rawIspInfo)
|| !is_string($rawIspInfo['org'])
|| empty($rawIspInfo['org'])
) {
return 'Unknown AS';
}
if (preg_match('/AS\\d+\\s/', $rawIspInfo['org'], $as) !== 1) {
return 'Unknown AS';
}
return trim($as['0']);
}
?>