$processedString, 'rawIspInfo' => $data ?: '', ]); } if (PHP_MAJOR_VERSION >= 8){ require_once("geoip2.phar"); } function getIspInfo_ipinfoOfflineDb($ip){ if (PHP_MAJOR_VERSION < 8 || !file_exists(OFFLINE_IPINFO_DB_FILE) || !is_readable(OFFLINE_IPINFO_DB_FILE)){ return null; } $reader = new MaxMind\Db\Reader(OFFLINE_IPINFO_DB_FILE); $data = $reader->get($ip); if(!is_array($data)){ return null; } $processedString = $ip.' - ' . $data['as_name'] . ', ' . $data['country_name']; return json_encode([ 'processedString' => $processedString, 'rawIspInfo' => $data ?: '', ]); } function formatResponse_simple($ip,$ispName=null){ $processedString=$ip; if(is_string($ispName)){ $processedString.=' - '.$ispName; } return json_encode([ 'processedString' => $processedString, 'rawIspInfo' => '', ]); } header('Content-Type: application/json; charset=utf-8'); if (isset($_GET['cors'])) { header('Access-Control-Allow-Origin: *'); header('Access-Control-Allow-Methods: GET, POST'); } header('Cache-Control: no-store, no-cache, must-revalidate, max-age=0, s-maxage=0'); header('Cache-Control: post-check=0, pre-check=0', false); header('Pragma: no-cache'); $ip = getClientIp(); //if the user requested the ISP info, we first try to fetch it using ipinfo.io (if there is no api key set it fails without sending data, it can also fail because of rate limiting or invalid responses), then we try with the offline db, if that also fails (or if ISP info was not requested) we just respond with the IP address if(isset($_GET['isp'])){ $localIpInfo = getLocalOrPrivateIpInfo($ip); //local ip, no need to fetch further information if (is_string($localIpInfo)) { echo formatResponse_simple($ip,$localIpInfo); }else{ $r=getIspInfo_ipinfoApi($ip); if(!is_null($r)){ echo $r; }else{ $r=getIspInfo_ipinfoOfflineDb($ip); if(!is_null($r)){ echo $r; }else{ echo formatResponse_simple($ip); } } } }else{ echo formatResponse_simple($ip); }