/* Gets a text description of the owner for an IP How to use this function: get_ip_owner('206.177.43.7'); Returns: string Yes, this was written very quickly. The code isn't pretty. */ $cached_ips; //Cache the IPs that are looked up to minimize ARIN API calls function get_ip_owner($ip){ global $cached_ips; $ip = trim($ip); if(strlen($ip) <= 16){ //IPs are always less than or equal to 16 characters //If after stripping all but [0-9]/. number is same then it's an IP if($ip == filter_var($ip,FILTER_SANITIZE_NUMBER_FLOAT,FILTER_FLAG_ALLOW_FRACTION)){ if(isset($cached_ips[$ip])) //Return out of cache if possible return $cached_ips[$ip]; $data = file_get_contents('http://whois.arin.net/rest/nets;q='.$ip.'?showDetails=true&showARIN=false&ext=netref2'); preg_match_all('/\(.*)<\/orgRef>/',$data,$matches); if(isset($matches[1][0])){ $xml = simplexml_load_string(file_get_contents($matches[1][0])); $info = $xml->city.': '; $info .= $xml->name.' ('.trim($xml->streetAddress->line[0]).', '.trim($xml->streetAddress->line[1]).')'; $info = str_replace(', )',')',$info); //Remove trailing punctuation $cached_ips[$ip] = $info; if(strlen($info)>3) //Make sure it's something return $info; }else{ $handle = $matches[0][0]; $handle = substr($handle,strpos($handle,'handle="')+strlen('handle="')); $handle = substr($handle,0,strpos($handle,'">')); if(strlen($info)>3) return $handle; } } } return $ip; //Not an IP address, spit input back out }