I needed this myself and thought it would be easy to find some scripts. It isn't.. so i wrote it myself. Any improvements are welcome.
<?
these functions show you where ip numbers come from geographically.
included is a small html form to test it.
Version 1.0 , 20020901
function ipwherewhois($ipaddr)
{
I only differentiate between Europe and the rest of the world. You can adapt the array
below to your own needs according to the document above.
$iplocation[62] = "eu";
$iplocation[80] = "eu";
$iplocation[81] = "eu";
$iplocation[193] = "eu";
$iplocation[194] = "eu";
$iplocation[195] = "eu";
$iplocation[212] = "eu";
$iplocation[213] = "eu";
$iplocation[217] = "eu";
$iparray = explode (".",$ipaddr);
$result = $iplocation[$iparray[0]]; if ($result == "") $result = "ww";
return $result;
}
function ipwherereverse($ipaddr)
{
@$tld = gethostbyaddr($ipaddr);
$result = strrchr($tld, ".");
$result = str_replace(".","",$result);
return $result;
}
function ipwhere($ipaddr)
{
the main function, which uses the other 2 functions as needed
returns a 2-letter country code TLD or eu (europe) or ww (worldwide)
$result = ipwherereverse($ipaddr);
if ((!$result) || (eregi("[0-9]{1,3}",$result)) || (strlen($result) > 2))
$result = ipwherewhois($ipaddr);
return $result;
}
and now for the testing lines:
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>IP address to geographical location</title>
</head>
<body>
<FORM ACTION=" <?PHP echo($PHP_SELF); ?>" METHOD="post">
Get country info from ip-address:<br><br>
ip-address: <INPUT TYPE="text" NAME="ipaddress" SIZE="40" MAXLENGTH="100" value="<?echo $ipaddress; ?>">
<INPUT TYPE=submit VALUE="Get location">
</FORM>
<?
if ($ipaddress)
{
$ipaddresswherefrom = ipwhere($ipaddress);
echo "The requested IP number corresponds to domain ";
@$dom = gethostbyaddr($ipaddress);
if ($dom == "") $dom = "unknown";
echo " $dom and comes from: $ipaddresswherefrom<br><br>";
}
$visitor = ipwhere($REMOTE_ADDR);
echo "You have IP number $REMOTE_ADDR and come from: $visitor <br>";
?>
</body>
</html>