/// <summary>
/// конвертирует ip-адрес в ip-номер
/// </summary>
/// <param name="ip">ip-адрес</param>
/// <returns></returns>
public string geoipusers(object ip)
{
int number = IPAddressToNumber(Convert.ToString(ip));
string res = GetUserCountry(number);
if (Convert.ToString(ip).StartsWith("127.0") || Convert.ToString(ip).StartsWith("192.168") || Convert.ToString(ip).StartsWith("10.19"))
return ip + " (локальный ip)";
if (res == null)
return ip + " (страна неизвестна)";
else
return ip + " (" + res + ")";
}
public int IPAddressToNumber(string IPaddress)
{
string[] arrDec;
int num = 0;
if (IPaddress != "")
{
arrDec = IPaddress.Split('.');
num = (int.Parse(arrDec[3])) + (int.Parse(arrDec[2]) * 256)
+ (int.Parse(arrDec[1]) * 65536) + (int.Parse(arrDec[0]) * 16777216);
}
return num;
}
/// <summary>
/// возвращает страну посетителя по номеру ip
/// </summary>
/// <returns></returns>
/// <param name="number">ip-номер</param>
public string GetUserCountry(int number)
{
SqlCommand cmd = new SqlCommand(string.Format("SELECT countryname FROM [IpToCountry] where '{0}' between beginingip and endingip", number));
return (string)Db.ExecuteScalar(cmd);
}