getIP.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. if (strpos($ip, '::1') !== false) {
  15. echo $ip . " - localhost ipv6 access";
  16. die();
  17. }
  18. if (strpos($ip, '127.0.0') !== false) {
  19. echo $ip . " - localhost ipv4 access";
  20. die();
  21. }
  22. /**
  23. * Optimized algorithm from http://www.codexworld.com
  24. *
  25. * @param float $latitudeFrom
  26. * @param float $longitudeFrom
  27. * @param float $latitudeTo
  28. * @param float $longitudeTo
  29. *
  30. * @return float [km]
  31. */
  32. function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) {
  33. $rad = M_PI / 180;
  34. $theta = $longitudeFrom - $longitudeTo;
  35. $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
  36. return acos($dist) / $rad * 60 * 1.853;
  37. }
  38. if (isset($_GET["isp"])) {
  39. $isp = "";
  40. $ip = (strpos($ip, ',') !== false) ? explode(', ',$ip)[1] : $ip;
  41. try {
  42. $json = @file_get_contents("https://ipinfo.io/" . $ip . "/json");
  43. $details = json_decode($json, true);
  44. if (is_array($details) || is_object($details)){
  45. if (array_key_exists("org", $details))
  46. $isp .= $details["org"];
  47. else
  48. $isp .= "Unknown ISP";
  49. if (array_key_exists("country", $details))
  50. $isp .= ", " . $details["country"];
  51. $clientLoc = NULL;
  52. $serverLoc = NULL;
  53. if (array_key_exists("loc", $details))
  54. $clientLoc = $details["loc"];
  55. if (isset($_GET["distance"])) {
  56. if ($clientLoc) {
  57. $json = file_get_contents("https://ipinfo.io/json");
  58. $details = json_decode($json, true);
  59. if (array_key_exists("loc", $details))
  60. $serverLoc = $details["loc"];
  61. if ($serverLoc) {
  62. try {
  63. $clientLoc = explode(",", $clientLoc);
  64. $serverLoc = explode(",", $serverLoc);
  65. $dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]);
  66. if ($_GET["distance"] == "mi") {
  67. $dist /= 1.609344;
  68. $dist = round($dist, -1);
  69. if ($dist < 15)
  70. $dist = "<15";
  71. $isp .= " (" . $dist . " mi)";
  72. }else if ($_GET["distance"] == "km") {
  73. $dist = round($dist, -1);
  74. if ($dist < 20)
  75. $dist = "<20";
  76. $isp .= " (" . $dist . " km)";
  77. }
  78. } catch (Exception $e) {
  79. }
  80. }
  81. }
  82. }
  83. }else{
  84. $isp = "Unknown ISP";
  85. }
  86. } catch (Exception $ex) {
  87. $isp = "Unknown ISP";
  88. }
  89. echo $ip . " - " . $isp;
  90. } else {
  91. echo $ip;
  92. }
  93. ?>