Sunday, December 12, 2010

IP to ISP/Country/City (GeoIP) using PHP

I've noted that many people are searching here about how to find out City/Country/ISP details from IP; often referred as GeoIP. Here, I've compiled my replies that once I posted to comp.lang.php

  1. Get the IP

  2. Refer and see which whois server should be used for the whois lookup

  3. Now, do the whois lookup. e.g., whois -h 61.x.x.x

  4. Parse the results. AFAIK, it will have the ISP, City and Country info

For whois lookup, may use rwhois protocol through below PHP code:

function whois($host, $command)
$fp = fsockopen ($host, 43, $errno, $errstr, 100);
if (!$fp) {
$result = $errstr . $errno . "\n";
} else {
fputs ($fp, $command . "\r\n\r\n");
$result = '';
while (!feof($fp)) {
$result .= fgets ($fp, 128);
fclose ($fp);
return $result;

echo whois('', '');

Related links

No comments: