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

Solved: "Ports" Tab Not Visible in VS Code (WSL2)

None of the solutions I found on Reddit or GitHub worked, and most of the related GitHub issues were locked. So I'm documenting this here for future reference. Problem When using WSL2 in Visual Studio Code, the Ports tab—needed to access your application in Windows browsers like Chrome or Edge—doesn't appear. Even the Ports-related options are missing from the Command Palette. Solution After a lot of trial and error, here's what worked: Run npx serve in the VS Code terminal. It will output a URL such as http://localhost:3000/ . Click the link (or use the Follow Link option). This will open the app in your default browser. You may notice that it opens on a different port (for example, http://localhost:64198 ). At this point, the Ports tab becomes available in VS Code. After this initial trigger, the Ports tab seems to remain available in future VS Code...

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

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

Shaping the Future: My Hopes and Dreams for My Children

I believe every parent has their own hopes for what their children might become, often shaped by personal experiences or insecurities. For instance, a janitor may wish for his child to achieve the success of his boss, and so on. My aspirations for my children stem from my own life and career experiences. There were many things I wanted to pursue but couldn’t, for various reasons. Over the years, I’ve suggested career paths to others based on their talents, and some have succeeded to the extent I anticipated. This gives me confidence in my ability to foresee what might be best for someone. For a long time, I’ve had ideas about my children’s future, and I want to put them in writing to see how things unfold. In short, I have a positive view of the freelance economy and royalty-based work. Royalty-based careers are a dream for many—creating something once and earning from it for a lifetime. I see similarities between Pillai and Heleena in how quickly they pick up new things. Pillai is a n...