You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

getIP.php 5.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
  11. $ip = $_SERVER['HTTP_CLIENT_IP'];
  12. } elseif (!empty($_SERVER['X-Real-IP'])) {
  13. $ip = $_SERVER['X-Real-IP'];
  14. } elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
  15. $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
  16. $ip = preg_replace("/,.*/", "", $ip); # hosts are comma-separated, client is first
  17. } else {
  18. $ip = $_SERVER['REMOTE_ADDR'];
  19. }
  20. $ip = preg_replace("/^::ffff:/", "", $ip);
  21. if ($ip == "::1") { // ::1/128 is the only localhost ipv6 address. there are no others, no need to strpos this
  22. echo json_encode(['processedString' => $ip . " - localhost IPv6 access", 'rawIspInfo' => ""]);
  23. die();
  24. }
  25. if (stripos($ip, 'fe80:') === 0) { // simplified IPv6 link-local address (should match fe80::/10)
  26. echo json_encode(['processedString' => $ip . " - link-local IPv6 access", 'rawIspInfo' => ""]);
  27. die();
  28. }
  29. if (strpos($ip, '127.') === 0) { //anything within the 127/8 range is localhost ipv4, the ip must start with 127.0
  30. echo json_encode(['processedString' => $ip . " - localhost IPv4 access", 'rawIspInfo' => ""]);
  31. die();
  32. }
  33. if (strpos($ip, '10.') === 0) { // 10/8 private IPv4
  34. echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]);
  35. die();
  36. }
  37. if (preg_match('/^172\.(1[6-9]|2\d|3[01])\./', $ip) === 1) { // 172.16/12 private IPv4
  38. echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]);
  39. die();
  40. }
  41. if (strpos($ip, '192.168.') === 0) { // 192.168/16 private IPv4
  42. echo json_encode(['processedString' => $ip . " - private IPv4 access", 'rawIspInfo' => ""]);
  43. die();
  44. }
  45. if (strpos($ip, '169.254.') === 0) { // IPv4 link-local
  46. echo json_encode(['processedString' => $ip . " - link-local IPv4 access", 'rawIspInfo' => ""]);
  47. die();
  48. }
  49. /**
  50. * Optimized algorithm from http://www.codexworld.com
  51. *
  52. * @param float $latitudeFrom
  53. * @param float $longitudeFrom
  54. * @param float $latitudeTo
  55. * @param float $longitudeTo
  56. *
  57. * @return float [km]
  58. */
  59. function distance($latitudeFrom, $longitudeFrom, $latitudeTo, $longitudeTo) {
  60. $rad = M_PI / 180;
  61. $theta = $longitudeFrom - $longitudeTo;
  62. $dist = sin($latitudeFrom * $rad) * sin($latitudeTo * $rad) + cos($latitudeFrom * $rad) * cos($latitudeTo * $rad) * cos($theta * $rad);
  63. return acos($dist) / $rad * 60 * 1.853;
  64. }
  65. if (isset($_GET["isp"])) {
  66. $isp = "";
  67. $rawIspInfo=null;
  68. try {
  69. $json = file_get_contents("https://ipinfo.io/" . $ip . "/json");
  70. $details = json_decode($json, true);
  71. $rawIspInfo=$details;
  72. if (array_key_exists("org", $details)){
  73. $isp .= $details["org"];
  74. $isp=preg_replace("/AS\d{1,}\s/","",$isp); //Remove AS##### from ISP name, if present
  75. }else{
  76. $isp .= "Unknown ISP";
  77. }
  78. if (array_key_exists("country", $details)){
  79. $isp .= ", " . $details["country"];
  80. }
  81. $clientLoc = NULL;
  82. $serverLoc = NULL;
  83. if (array_key_exists("loc", $details)){
  84. $clientLoc = $details["loc"];
  85. }
  86. if (isset($_GET["distance"])) {
  87. if ($clientLoc) {
  88. $json = file_get_contents("https://ipinfo.io/json");
  89. $details = json_decode($json, true);
  90. if (array_key_exists("loc", $details)){
  91. $serverLoc = $details["loc"];
  92. }
  93. if ($serverLoc) {
  94. try {
  95. $clientLoc = explode(",", $clientLoc);
  96. $serverLoc = explode(",", $serverLoc);
  97. $dist = distance($clientLoc[0], $clientLoc[1], $serverLoc[0], $serverLoc[1]);
  98. if ($_GET["distance"] == "mi") {
  99. $dist /= 1.609344;
  100. $dist = round($dist, -1);
  101. if ($dist < 15)
  102. $dist = "<15";
  103. $isp .= " (" . $dist . " mi)";
  104. }else if ($_GET["distance"] == "km") {
  105. $dist = round($dist, -1);
  106. if ($dist < 20)
  107. $dist = "<20";
  108. $isp .= " (" . $dist . " km)";
  109. }
  110. } catch (Exception $e) {
  111. }
  112. }
  113. }
  114. }
  115. } catch (Exception $ex) {
  116. $isp = "Unknown ISP";
  117. }
  118. echo json_encode(['processedString' => $ip . " - " . $isp, 'rawIspInfo' => $rawIspInfo]);
  119. } else {
  120. echo json_encode(['processedString' => $ip, 'rawIspInfo' => ""]);
  121. }
  122. ?>