1
0

getIP.php 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. $ip="";
  3. header('Content-Type: text/plain; charset=utf-8');
  4. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  5. $ip=$_SERVER['HTTP_CLIENT_IP'];
  6. } elseif (!empty($_SERVER['X-Real-IP'])) {
  7. $ip=$_SERVER['X-Real-IP'];
  8. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  9. $ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
  10. } else {
  11. $ip=$_SERVER['REMOTE_ADDR'];
  12. }
  13. $ip=preg_replace("/^::ffff:/", "", $ip);
  14. /**
  15. * Optimized algorithm from http://www.codexworld.com
  16. *
  17. * @param float $latitudeFrom
  18. * @param float $longitudeFrom
  19. * @param float $latitudeTo
  20. * @param float $longitudeTo
  21. *
  22. * @return float [km]
  23. */
  24. function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo){
  25. $rad = M_PI / 180;
  26. $theta = $longitudeFrom - $longitudeTo;
  27. $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
  28. return acos($dist) / $rad * 60 * 1.853;
  29. }
  30. if(isset($_GET["isp"])){
  31. $isp="";
  32. try{
  33. $json = file_get_contents("https://ipinfo.io/".$ip."/json");
  34. $details = json_decode($json,true);
  35. if(array_key_exists("org",$details)) $isp.=$details["org"]; else $isp.="Unknown ISP";
  36. if(array_key_exists("country",$details)) $isp.=", ".$details["country"];
  37. $clientLoc=NULL; $serverLoc=NULL;
  38. if(array_key_exists("loc",$details)) $clientLoc=$details["loc"];
  39. if(isset($_GET["distance"])){
  40. if($clientLoc){
  41. $json = file_get_contents("https://ipinfo.io/json");
  42. $details = json_decode($json,true);
  43. if(array_key_exists("loc",$details)) $serverLoc=$details["loc"];
  44. if($serverLoc){
  45. try{
  46. $clientLoc=explode(",",$clientLoc);
  47. $serverLoc=explode(",",$serverLoc);
  48. $dist=distance($clientLoc[0],$clientLoc[1],$serverLoc[0],$serverLoc[1]);
  49. if($_GET["distance"]=="mi"){
  50. $dist/=1.609344;
  51. $dist=round($dist,-1);
  52. if($dist<15) $dist="<15";
  53. $isp.=" (".$dist." mi)";
  54. }else if($_GET["distance"]=="km"){
  55. $dist=round($dist,-1);
  56. if($dist<20) $dist="<20";
  57. $isp.=" (".$dist." km)";
  58. }
  59. }catch(Exception $e){}
  60. }
  61. }
  62. }
  63. }catch(Exception $ex){
  64. $isp="Unknown ISP";
  65. }
  66. echo $ip." - ".$isp;
  67. } else echo $ip;
  68. ?>