Avoid requesting ipinfo.io and generating a php error when the connection is from localhost (#135)
This commit is contained in:
parent
5cae01960b
commit
8c27da939b
120
getIP.php
120
getIP.php
|
@ -1,17 +1,29 @@
|
||||||
<?php
|
<?php
|
||||||
$ip="";
|
|
||||||
header('Content-Type: text/plain; charset=utf-8');
|
$ip = "";
|
||||||
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
header('Content-Type: text/plain; charset=utf-8');
|
||||||
$ip=$_SERVER['HTTP_CLIENT_IP'];
|
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
|
||||||
} elseif (!empty($_SERVER['X-Real-IP'])) {
|
$ip = $_SERVER['HTTP_CLIENT_IP'];
|
||||||
$ip=$_SERVER['X-Real-IP'];
|
} elseif (!empty($_SERVER['X-Real-IP'])) {
|
||||||
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
$ip = $_SERVER['X-Real-IP'];
|
||||||
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
|
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
|
||||||
} else {
|
$ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
|
||||||
$ip=$_SERVER['REMOTE_ADDR'];
|
} else {
|
||||||
}
|
$ip = $_SERVER['REMOTE_ADDR'];
|
||||||
$ip=preg_replace("/^::ffff:/", "", $ip);
|
}
|
||||||
/**
|
|
||||||
|
$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
|
* Optimized algorithm from http://www.codexworld.com
|
||||||
*
|
*
|
||||||
* @param float $latitudeFrom
|
* @param float $latitudeFrom
|
||||||
|
@ -21,48 +33,62 @@
|
||||||
*
|
*
|
||||||
* @return float [km]
|
* @return float [km]
|
||||||
*/
|
*/
|
||||||
function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo){
|
function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) {
|
||||||
$rad = M_PI / 180;
|
$rad = M_PI / 180;
|
||||||
$theta = $longitudeFrom - $longitudeTo;
|
$theta = $longitudeFrom - $longitudeTo;
|
||||||
$dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
|
$dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
|
||||||
return acos($dist) / $rad * 60 * 1.853;
|
return acos($dist) / $rad * 60 * 1.853;
|
||||||
}
|
}
|
||||||
if(isset($_GET["isp"])){
|
|
||||||
$isp="";
|
if (isset($_GET["isp"])) {
|
||||||
try{
|
$isp = "";
|
||||||
$json = file_get_contents("https://ipinfo.io/".$ip."/json");
|
try {
|
||||||
$details = json_decode($json,true);
|
$json = file_get_contents("https://ipinfo.io/" . $ip . "/json");
|
||||||
if(array_key_exists("org",$details)) $isp.=$details["org"]; else $isp.="Unknown ISP";
|
$details = json_decode($json, true);
|
||||||
if(array_key_exists("country",$details)) $isp.=", ".$details["country"];
|
if (array_key_exists("org", $details))
|
||||||
$clientLoc=NULL; $serverLoc=NULL;
|
$isp .= $details["org"];
|
||||||
if(array_key_exists("loc",$details)) $clientLoc=$details["loc"];
|
else
|
||||||
if(isset($_GET["distance"])){
|
$isp .= "Unknown ISP";
|
||||||
if($clientLoc){
|
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");
|
$json = file_get_contents("https://ipinfo.io/json");
|
||||||
$details = json_decode($json,true);
|
$details = json_decode($json, true);
|
||||||
if(array_key_exists("loc",$details)) $serverLoc=$details["loc"];
|
if (array_key_exists("loc", $details))
|
||||||
if($serverLoc){
|
$serverLoc = $details["loc"];
|
||||||
try{
|
if ($serverLoc) {
|
||||||
$clientLoc=explode(",",$clientLoc);
|
try {
|
||||||
$serverLoc=explode(",",$serverLoc);
|
$clientLoc = explode(",", $clientLoc);
|
||||||
$dist=distance($clientLoc[0],$clientLoc[1],$serverLoc[0],$serverLoc[1]);
|
$serverLoc = explode(",", $serverLoc);
|
||||||
if($_GET["distance"]=="mi"){
|
$dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]);
|
||||||
$dist/=1.609344;
|
if ($_GET["distance"] == "mi") {
|
||||||
$dist=round($dist,-1);
|
$dist /= 1.609344;
|
||||||
if($dist<15) $dist="<15";
|
$dist = round($dist, -1);
|
||||||
$isp.=" (".$dist." mi)";
|
if ($dist < 15)
|
||||||
}else if($_GET["distance"]=="km"){
|
$dist = "<15";
|
||||||
$dist=round($dist,-1);
|
$isp .= " (" . $dist . " mi)";
|
||||||
if($dist<20) $dist="<20";
|
}else if ($_GET["distance"] == "km") {
|
||||||
$isp.=" (".$dist." km)";
|
$dist = round($dist, -1);
|
||||||
|
if ($dist < 20)
|
||||||
|
$dist = "<20";
|
||||||
|
$isp .= " (" . $dist . " km)";
|
||||||
}
|
}
|
||||||
}catch(Exception $e){}
|
} catch (Exception $e) {
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}catch(Exception $ex){
|
|
||||||
$isp="Unknown ISP";
|
|
||||||
}
|
}
|
||||||
echo $ip." - ".$isp;
|
} catch (Exception $ex) {
|
||||||
} else echo $ip;
|
$isp = "Unknown ISP";
|
||||||
|
}
|
||||||
|
echo $ip . " - " . $isp;
|
||||||
|
} else {
|
||||||
|
echo $ip;
|
||||||
|
}
|
||||||
?>
|
?>
|
||||||
|
|
Loading…
Reference in a new issue