$ip . " - localhost IPv6 access", 'rawIspInfo' => ""]); die(); } if (stripos($ip, 'fe80:') === 0) { // simplified IPv6 link-local address (should match fe80::/10) echo json_encode(['processedString' => $ip . " - link-local IPv6 access", 'rawIspInfo' => ""]); die(); } if (strpos($ip, '127.') === 0) { //anything within the 127/8 range is localhost ipv4, the ip must start with 127.0 echo json_encode(['processedString' => $ip . " - localhost IPv4 access", 'rawIspInfo' => ""]); die(); } if (strpos($ip, '10.') === 0) { // 10/8 private IPv4 echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]); die(); } if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) { // 172.16/12 private IPv4 echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]); die(); } if (strpos($ip, '192.168.') === 0) { // 192.168/16 private IPv4 echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]); die(); } if (strpos($ip, '169.254.') === 0) { // IPv4 link-local echo json_encode(['processedString' => $ip . " - link-local IPv4 access", 'rawIspInfo' => ""]); die(); } /** * Optimized algorithm from http://www.codexworld.com * * @param float $latitudeFrom * @param float $longitudeFrom * @param float $latitudeTo * @param float $longitudeTo * * @return float [km] */ function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) { $rad = M_PI / 180; $theta = $longitudeFrom - $longitudeTo; $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad); return acos($dist) / $rad * 60 * 1.853; } if (isset($_GET["isp"])) { $isp = ""; $rawIspInfo=null; try { $json = file_get_contents("https://ipinfo.io/" . $ip . "/json"); $details = json_decode($json, true); $rawIspInfo=$details; if (array_key_exists("org", $details)){ $isp .= $details["org"]; $isp=preg_replace("/AS\d{1,}\s/","",$isp); //Remove AS##### from ISP name, if present }else{ $isp .= "Unknown ISP"; } if (array_key_exists("country", $details)){ $isp .= ", " . $details["country"]; } $clientLoc = NULL; $serverLoc = NULL; if (array_key_exists("loc", $details)){ $clientLoc = $details["loc"]; } if (isset($_GET["distance"])) { if ($clientLoc) { $json = file_get_contents("https://ipinfo.io/json"); $details = json_decode($json, true); if (array_key_exists("loc", $details)){ $serverLoc = $details["loc"]; } if ($serverLoc) { try { $clientLoc = explode(",", $clientLoc); $serverLoc = explode(",", $serverLoc); $dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]); if ($_GET["distance"] == "mi") { $dist /= 1.609344; $dist = round($dist, -1); if ($dist < 15) $dist = "<15"; $isp .= " (" . $dist . " mi)"; }else if ($_GET["distance"] == "km") { $dist = round($dist, -1); if ($dist < 20) $dist = "<20"; $isp .= " (" . $dist . " km)"; } } catch (Exception $e) { } } } } } catch (Exception $ex) { $isp = "Unknown ISP"; } echo json_encode(['processedString' => $ip . " - " . $isp, 'rawIspInfo' => $rawIspInfo]); } else { echo json_encode(['processedString' => $ip, 'rawIspInfo' => ""]); } ?>