<?php
$ip = "8.8.8.8"."\r\n";
echo whois_get($ip);
//Open connection to whois server and fetch data
function whois_query($ip, $server) {
$fp = @fsockopen($server, 43, $errno, $errdesc);
$answer = '';
while (!feof($fp)) {
$answer .= fread($fp, 8192);
}
fclose($fp);
return $answer;
echo $answer;
};
function whois_get($ip) {
$string = whois_query($ip, 'whois.iana.org');
preg_match('/whois\..+/', $string, $whoisServer);
if (array_key_exists('0', $whoisServer)) {
if ($whoisServer[0] == "whois.arin.net") {
$ip = "n ".$ip;
}
$whoisInfo = whois_query($ip, $whoisServer[0]);
$country = getCountry($whoisInfo);
return "<b>Country:</b>".$country." \r\n";
} else {
return "UNKNOWN";
}
}
function getCountry($data){
preg_match('/ountry\:\s*[A-Z]{2}/', $data, $country);
$whoisCountry = $country[0];
$whoisCountry = preg_replace('/ountry\:/', '', $whoisCountry);
$whoisCountry = preg_replace('/\s+/', '', $whoisCountry);
return $whoisCountry;
}