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

Converting PSD with PHP/ImageMagick

After seeing feature rich options in Imagick PECL extension at Mikko Koppanen 's (the author) website and also impressed with ImageMagick 's features, I have decided to use it for the PSD to XHTML conversion website that I'm architecting and managing. Since, the team wants programming help for converting PSD images, I have tried it (documentation is sparse on PSD handling) Converting PSD to PNG/JPEG/etc Note that, flattenImages() is needed for layered/multi-page PSD file. <?php $im = new Imagick('test.psd'); $im->flattenImages(); $im->setImageFormat('png'); $im->writeImage('test.png'); ?> Extracting PSD layers One by one <?php $im = new Imagick('test.psd'); $im->setImageFormat('png'); for ($i = 0, $num_layers = $im->getNumberImages(); $i $im->setImageIndex($i); $im->writeImage('layer' . $i . '.png'); } ?> Note that, there is a better version below In a single call with writeIm...

Google used PHP?

I wanted to know what are the pages in Google are powered by PHP . I have tried various keywords including "inurl"--but it produced huge list. So I tried "filetype:php" and it worked very nice. The real keywords are php filetype:php site:google.com Google used PHP?

Work avoiders on the rise

roughly 80% of the effects come from 20% of the causes --  80–20 rule / Pareto principle One popular store (that is known for its less selling price, 6-storied air-conditioned showroom) is situated to close to our home. But, Heleena is allergic to it. Reason? Even though there are at least 50 sales people per floor, only 1 or 2 people will be working and responding to the customers' queries. In another shop, I even noticed that the owner was serving customer when many staffs were cleverly avoiding the show! I'm finding the same problem everywhere. Who're affected here? Obviously the people who're actually working--they will have to handle more work that the clever people have avoided. Two common gestures/characteristics I have noted in these clever people: 1. They'll pretend to be absolute ignorant (These people's agenda are merely to avoid works, but not to take credits.), 2. They'll create politics on anything (These people's agenda ar...