Skip to main content

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 http://www.iana.org/assignments/ipv4-address-space/ipv4-address-space.xml and see which whois server should be used for the whois lookup

  3. Now, do the whois lookup. e.g., whois -h whois.apnic.net 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:



<?php
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;
}

//debug...
echo whois('whois.internic.net', 'php.net');
?>



Related links


Comments

Popular posts from this blog

I'm Zend (PHP 5) Certified Engineer:-)

As of January 13, 2007, I'm ZCE/ Zend Certified Engineer (PHP 5)

RTaKeyman Bookmarklet v2.0

RTaKeyman The above bookmarklet is for English to Tamil transcription. This should work like a Input Method Editor on web page forms. The first version I tried didn't work well and so this bookmarklet version. Version 2.0 2004-12-27 1.0 2004-12-12 Usage Make a bookmarklet with the above link. Point to a web page where you want to enter Tamil characters and click the bookmarklet. This will initialize the keyman for that page. Then start typing on the forms; RTaKeyman will transcribe the characters whenever it sees a space, carriage return and tab. To-do Turning on/off keyman (i.e., to enter both Tamil and English). Help and other user interfaces. Credits Sundar for informing the necessity and for being a beta tester

Prediction: Expensify will crash through its insane question-based hiring process

 I have huge respect for  David Barrett for his technical background. But, his company, Expensify is following a modern  psychometric test called a  question-based hiring process . The company claims to look for candidates with 'Talent, Ambition, and Humility'. The psychometric test was a fad in India about 20-years ago and the companies that followed the hiring approaches are no more and some have changed the approaches to survive. I just thought of blogging my thoughts here to see how Expensify evolves.