Avoid requesting ipinfo.io and generating a php error when the connection is from localhost (#135)

This commit is contained in:
V. D'AGOSTINO 2018-04-25 21:03:18 +02:00 committed by Federico Dossena
parent 5cae01960b
commit 8c27da939b

View file

@ -1,4 +1,5 @@
<?php
$ip = "";
header('Content-Type: text/plain; charset=utf-8');
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
@ -10,7 +11,18 @@
} else {
$ip = $_SERVER['REMOTE_ADDR'];
}
$ip = preg_replace("/^::ffff:/", "", $ip);
if (strpos($ip, '::1') !== false) {
echo $ip . " - localhost ipv6 access";
die();
}
if (strpos($ip, '127.0.0') !== false) {
echo $ip . " - localhost ipv4 access";
die();
}
/**
* Optimized algorithm from http://www.codexworld.com
*
@ -27,20 +39,28 @@
$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 = "";
try {
$json = file_get_contents("https://ipinfo.io/" . $ip . "/json");
$details = json_decode($json, true);
if(array_key_exists("org",$details)) $isp.=$details["org"]; 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 (array_key_exists("org", $details))
$isp .= $details["org"];
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 (array_key_exists("loc", $details))
$serverLoc = $details["loc"];
if ($serverLoc) {
try {
$clientLoc = explode(",", $clientLoc);
@ -49,14 +69,18 @@
if ($_GET["distance"] == "mi") {
$dist /= 1.609344;
$dist = round($dist, -1);
if($dist<15) $dist="<15";
if ($dist < 15)
$dist = "<15";
$isp .= " (" . $dist . " mi)";
}else if ($_GET["distance"] == "km") {
$dist = round($dist, -1);
if($dist<20) $dist="<20";
if ($dist < 20)
$dist = "<20";
$isp .= " (" . $dist . " km)";
}
}catch(Exception $e){}
} catch (Exception $e) {
}
}
}
}
@ -64,5 +88,7 @@
$isp = "Unknown ISP";
}
echo $ip . " - " . $isp;
} else echo $ip;
} else {
echo $ip;
}
?>