If you need to detect from which country your visitors are connecting to your website follow these 4 steps:

1) wget http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip
2) unzip ip-to-country.csv.zip
3) chmod 777 ip-to-country.csv
4) MySQL:

  1. CREATE DATABASE ip2country;
  2. CREATE TABLE `ip2country` ( `ipFrom` int(15) NOT NULL default ‘0′, `ipTo` int(15) NOT NULL default ‘0′, `country2` char(2) NOT NULL default ”, `country3` char(3) NOT NULL default ”, `country` varchar(25) NOT NULL default ”);

  3. load data infile ‘/home/downloads/ip-to-country.csv’ into table ip2country fields terminated by ‘,’ enclosed by ‘”‘ lines terminated by ‘\n’;

To test if everything is working create a PHP file with the following code:

<?

$ip = $_SERVER['REMOTE_ADDR'];

$dbh = mysql_connect(“localhost”, “username”, “Password”) or die(“Could not connect: ” . mysql_error());

mysql_select_db(“ip2country”);

$country_query = “SELECT country2, country FROM ip2country WHERE ipFrom<=INET_ATON(‘” . $ip . “‘) AND ipTo>=INET_ATON(‘” . $ip . “‘)”;

$country_exec = mysql_query($country_query);

$ccode_array = mysql_fetch_array($country_exec);

mysql_close($dbh);

$country_code = $ccode_array['country2'];

$country_name = $ccode_array['country'];

echo “Country: $country_name ($country_code)”;

?>
Follow me on Twitter