getIP.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. <?php
  2. /*
  3. This script detects the client's IP address and fetches ISP info from ipinfo.io/
  4. Output from this script is a JSON string composed of 2 objects: a string called processedString which contains the combined IP, ISP, Contry and distance as it can be presented to the user; and an object called rawIspInfo which contains the raw data from ipinfo.io (will be empty if isp detection is disabled).
  5. Client side, the output of this script can be treated as JSON or as regular text. If the output is regular text, it will be shown to the user as is.
  6. */
  7. error_reporting(0);
  8. $ip = "";
  9. header('Content-Type: application/json; charset=utf-8');
  10. header('Access-Control-Allow-Origin: *');
  11. header('Access-Control-Allow-Methods: GET, POST');
  12. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  13. $ip = $_SERVER['HTTP_CLIENT_IP'];
  14. } elseif (!empty($_SERVER['X-Real-IP'])) {
  15. $ip = $_SERVER['X-Real-IP'];
  16. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  17. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  18. $ip = preg_replace("/,.*/", "", $ip); # hosts are comma-separated, client is first
  19. } else {
  20. $ip = $_SERVER['REMOTE_ADDR'];
  21. }
  22. $ip = preg_replace("/^::ffff:/", "", $ip);
  23. if (strpos($ip, '::1') !== false) {
  24. echo json_encode(['processedString' => $ip . " - localhost ipv6 access", 'rawIspInfo' => ""]);
  25. die();
  26. }
  27. if (strpos($ip, '127.0.0') !== false) {
  28. echo json_encode(['processedString' => $ip . " - localhost ipv4 access", 'rawIspInfo' => ""]);
  29. die();
  30. }
  31. /**
  32. * Optimized algorithm from http://www.codexworld.com
  33. *
  34. * @param float $latitudeFrom
  35. * @param float $longitudeFrom
  36. * @param float $latitudeTo
  37. * @param float $longitudeTo
  38. *
  39. * @return float [km]
  40. */
  41. function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) {
  42. $rad = M_PI / 180;
  43. $theta = $longitudeFrom - $longitudeTo;
  44. $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
  45. return acos($dist) / $rad * 60 * 1.853;
  46. }
  47. if (isset($_GET["isp"])) {
  48. $isp = "";
  49. $rawIspInfo=null;
  50. try {
  51. $json = file_get_contents("https://ipinfo.io/" . $ip . "/json");
  52. $details = json_decode($json, true);
  53. $rawIspInfo=$details;
  54. if (array_key_exists("org", $details))
  55. $isp .= $details["org"];
  56. else
  57. $isp .= "Unknown ISP";
  58. if (array_key_exists("country", $details))
  59. $isp .= ", " . $details["country"];
  60. $clientLoc = NULL;
  61. $serverLoc = NULL;
  62. if (array_key_exists("loc", $details))
  63. $clientLoc = $details["loc"];
  64. if (isset($_GET["distance"])) {
  65. if ($clientLoc) {
  66. $json = file_get_contents("https://ipinfo.io/json");
  67. $details = json_decode($json, true);
  68. if (array_key_exists("loc", $details))
  69. $serverLoc = $details["loc"];
  70. if ($serverLoc) {
  71. try {
  72. $clientLoc = explode(",", $clientLoc);
  73. $serverLoc = explode(",", $serverLoc);
  74. $dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]);
  75. if ($_GET["distance"] == "mi") {
  76. $dist /= 1.609344;
  77. $dist = round($dist, -1);
  78. if ($dist < 15)
  79. $dist = "<15";
  80. $isp .= " (" . $dist . " mi)";
  81. }else if ($_GET["distance"] == "km") {
  82. $dist = round($dist, -1);
  83. if ($dist < 20)
  84. $dist = "<20";
  85. $isp .= " (" . $dist . " km)";
  86. }
  87. } catch (Exception $e) {
  88. }
  89. }
  90. }
  91. }
  92. } catch (Exception $ex) {
  93. $isp = "Unknown ISP";
  94. }
  95. echo json_encode(['processedString' => $ip . " - " . $isp, 'rawIspInfo' => $rawIspInfo]);
  96. } else {
  97. echo json_encode(['processedString' => $ip, 'rawIspInfo' => ""]);
  98. }
  99. ?>