Find the Country from IP

You can find the latest IP range database here
http://ip-to-country.webhosting.info/book/print/5

http://ip-to-country.webhosting.info/downloads/ip-to-country.csv.zip?XID=1f1891875b75215ed000793c0fff1c20



In the datbase IP range will be there with the IP Number.

To find the real IP use the following php function:

function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
$ip=$_SERVER['REMOTE_ADDR'];
return $ip;
}



To find the IP Number use the following php function

function ip2num($ipAddr){
$arr=explode('.',$ipAddr);
$ip=($arr[0]*256*256*256)+($arr[1]*256*256)+($arr[2]*256)+$arr[3];
return $ip;
}

You will get a IP number.

Extract the database and and export it your database in to a table
then write a sql query with between ip_from and ip_to range using the ip number which you got to find the country code.