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

Interview question #1

Since I have been asked to interview experienced PHP programmers, I was preparing few interview questions. I came to know, most of the people ask questions found in the Internet; most of them are like "What is the function used to connect MySQL DB in PHP". Personally, I don't like these types of questions; I'd thought the person must apply ideas what he was taught in colleges--finally I came out with one question: A product vendor has Quantity Vs. Price data like 1 -> Rs. 50 2 -> Rs. 95 3 -> Rs. 140 .... like upto 1 million data. He wants a system, which gives the price when the quantity is provided. For example, if you provide the quantity value as 2, then it should provide Rs. 95. How this system can be designed? As expected, all the people said about using database tables and quering on quantity. I have asked them to find out a system which doesn't use databases--provided the accuracy of the system may not be 100%--it may give at least 90% ac...

Valen Smith, the English teacher videos are missing!

I vaguely remembered that sometimes around 2009, engVid on YouTube was very popular. I especially liked a teacher by name Valen Smith's videos. She was not only beautiful but also very good at explaining the English usage. I also vaguely remembered to check her channel ValenESL around 2015. But, not sure what happened to her or engVid, all her videos are sadly gone. I have also noted that many people on Twitter were asking the same. Not sure what happened. Some people seem to have reuploaded her videos and YouTube continuously removing them with a note "This video contains content from LearnVid, who has blocked it on copyright grounds." Strangely though, on Facebook, some people have managed to create a fake account using her old channel name ValenESL.