getIP.php 6.0 KB

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