code to detect the ip address:
public string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}
conver ip address to ip number:
public double IPAddressToNumber(string IPaddress)
{
int i;
string [] arrDec;
double num = 0;
if (IPaddress == "")
{
return 0;
}
else
{
arrDec = IPaddress.Split('.');
for(i = arrDec.Length - 1; i >= 0 ; i --)
{
num += ((int.Parse(arrDec[i])%256) * Math.Pow(256 ,(3 - i )));
}
return num;
}
}
to find the country :
Example: the IP Address 202.186.13.4 converts to IP Number 3401190660. It is between the beginning and the ending of the following IP numbers:
"3401056256","3401400319","MY","MALAYSIA"
From the IP-Country recordset, the Country_Name with this IP number range is Malaysia, the Country_Code is MY.
So once you have the IP Number, here's the SQL Query to locate the matching recordset:
SELECT country_name FROM ip_to_country WHERE
[your IP number] BETWEEN ip_start AND ip_end