symdesign
3/6/2015 - 8:26 PM

Visitor's country

Find out the visitor's country without additional php extensions or databases

<?php
$ip = $_SERVER['REMOTE_ADDR'];

$host = gethostbyaddr($ip);
$host = strtolower($host);
$hostTrans = array(
  '.arcor-ip.net'=>'.de',  '.t-dialin.net'=>'.de',
  '.sui-inter.net'=>'.ch', '.drei.com'=>'.at',
  '.proxad.net'=>'.fr',    '.gaoland.net'=>'.fr',
  '.mchsi.com'=>'.us',     '.comcast.net'=>'.us',
  );
$host = strtr($host, $hostTrans);

$country = (strpos($host, '.')===false) ?
           $host :
           substr(strrchr($host, '.'), 1);

if($ip === $host)
  $country = '?';

# possible outputs of $country:
# de, uk, com, ?, localhost
?>