getIP.php 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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. try {
  41. $json = file_get_contents("https://ipinfo.io/" . $ip . "/json");
  42. $details = json_decode($json, true);
  43. if (array_key_exists("org", $details))
  44. $isp .= $details["org"];
  45. else
  46. $isp .= "Unknown ISP";
  47. if (array_key_exists("country", $details))
  48. $isp .= ", " . $details["country"];
  49. $clientLoc = NULL;
  50. $serverLoc = NULL;
  51. if (array_key_exists("loc", $details))
  52. $clientLoc = $details["loc"];
  53. if (isset($_GET["distance"])) {
  54. if ($clientLoc) {
  55. $json = file_get_contents("https://ipinfo.io/json");
  56. $details = json_decode($json, true);
  57. if (array_key_exists("loc", $details))
  58. $serverLoc = $details["loc"];
  59. if ($serverLoc) {
  60. try {
  61. $clientLoc = explode(",", $clientLoc);
  62. $serverLoc = explode(",", $serverLoc);
  63. $dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]);
  64. if ($_GET["distance"] == "mi") {
  65. $dist /= 1.609344;
  66. $dist = round($dist, -1);
  67. if ($dist < 15)
  68. $dist = "<15";
  69. $isp .= " (" . $dist . " mi)";
  70. }else if ($_GET["distance"] == "km") {
  71. $dist = round($dist, -1);
  72. if ($dist < 20)
  73. $dist = "<20";
  74. $isp .= " (" . $dist . " km)";
  75. }
  76. } catch (Exception $e) {
  77. }
  78. }
  79. }
  80. }
  81. } catch (Exception $ex) {
  82. $isp = "Unknown ISP";
  83. }
  84. echo $ip . " - " . $isp;
  85. } else {
  86. echo $ip;
  87. }
  88. ?>