Re: Geo IP Location help needed...
| From: | Daniel Brown | Date: | Tue, 25 Oct 2011 12:58:31 +0000 |
| Subject: | Re: Geo IP Location help needed... | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-315419@lists.php.net to get a copy of this message | ||
On Mon, Oct 24, 2011 at 21:07, DealTek <dealtek@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/