On Mon, Oct 24, 2011 at 21:07, DealTek <deal...@gmail.com> wrote:
>
> Q: Is there some code that can ALWAYS tell the country name of the IP?

    More often than not, you will get accurate, real-time (as in, not
static and potentially stale from a database) results using a direct
WHOIS query.  If you're on Linux without safe_mode enabled in PHP:

<?php

$ip = $_SERVER['REMOTE_ADDR'];
$country = trim(`whois $ip | grep -i country`);

if (!isset($country)) {
    echo 'Country undetermined for IP: '.$ip.PHP_EOL;
} else {
    echo $country.PHP_EOL;
}
?>

    The above will work in situations where some copies of a geoIP
database may not.  An example of this would be recent allocations of
the 184 blocks.  Plus, if an IP is allocated to an entity in the US,
you can get the state to which the IP was registered.  Note that, in
many cases, this isn't the state in which the IP actually terminates,
but in the case of small and local ISPs, academic institutions, et
cetera, it's pretty accurate.

-- 
</Daniel P. Brown>
Network Infrastructure Manager
http://www.php.net/

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to