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

"PHP 5 Power Programming" Free in PDF

The book PHP 5 Power Programming is now available free of cost in downloadable PDF format at Bruce Perens' Open Source Series promotion site. Along with this book, a lot of other books are also available there for free! Details ISBN: 0-13-147149-X 2005. Pages: 720 Authors: Andi Gutmans , Stig Sæther Bakken , and Derick Rethans Download location: Perens Series Page (PDF) License: Open Publication License File size: 9.6MB Original price: $39.99 Edition: 2005

BehaviorS.js - An alternative to Behaviour.js, event:Selectors and Low Pro libs for unobtrusive JavaScript programming

BehaviorS.js yet another unobtrusive JavaScript library similar to Behaviour.js and event:Selectors but in implementation uses hash based lookup without extending elements; so presumably it should be faster than the rest. The original script and idea was by JLof ; I extended it for DOMContentLoaded support, optimized a bit to avoid scanning of more depths, and added new rules support. I wanted to document the plug a long time and just got time to do it. For the time being BehaviorS.js is available here Update (2006-09-11) : Coralized the link to BehaviorS.js so as to save the load on free brinkster.com webpage Update (2006-09-27) : If the coralized link to BehaviorS.js doesn't work, use http://www21.brinkster.com/guideme/BehaviorS/

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

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