.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Displaying Country With flag Counter

Posted By:Gowthammanju       Posted Date: August 29, 2011    Points: 200    Category: ASP.NET    URL: http://www.dotnetspark.com  

This article explains about displaying the total visitor from different countries visited our web page
 

Hi,

This article explains about total visitor of the particular country hits our page.

1) First download all flag images from Icons attached below
2)Next Get all Ip data address which is attached below site
3)Now use following coding in your aspx code page as
protected void Page_Load(object sender, EventArgs e)
{
string visitorIp = ReturnAlphaNumeric(Request.QueryString["ip"]);
if (visitorIp==null)
visitorIp = Request.ServerVariables["REMOTE_ADDR"];
 
Response.StatusCode = 200;
Response.Cache.SetExpires(DateTime.Now.AddMinutes(10));
Response.Cache.SetCacheability(HttpCacheability.Public);
 
 
try
{
Response.ContentType = "image/png";
 
FileStream flag = new FileStream(Server.MapPath("flags/" + GetCountryFlag(visitorIp) + ".png"), FileMode.Open, FileAccess.Read, FileShare.Read);
long FileSize;
FileSize = flag.Length;
byte[] getContent = new byte[(int)FileSize];
flag.Read(getContent, 0, (int)flag.Length);
flag.Close();
Response.BinaryWrite(getContent);
 
}
catch (Exception ee)
{
Response.ContentType = "text/html";
Response.Write(ee.ToString());
}
 
}
 
public static string ReturnAlphaNumeric(string input)
{
try
{
return Regex.Replace(input, @"[^0-9\.]", "").ToString();
}
catch
{
return "127.0.0.1";
}
}
 
public static string GetCountryFlag(string ip)
{
string cc = "-";
//string cn = "Unknown";
 
using (CountryLookup cl = new CountryLookup(HttpContext.Current.Server.MapPath("GeoIP.dat")))
{
try
{
cc = cl.lookupCountryCode(ip);
}
catch (Exception ex)
{
cc = "-";
}
}
 
return cc;
 
}
 
public class CountryLookup : IDisposable
{
 
private FileStream fileInput;
private bool _close;
private static long COUNTRY_BEGIN = 16776960;
private static string[] countryCode = 
{ "-","AP","EU","AD","AE","AF","AG","AI","AL","AM","AN","AO","AQ","AR","AS","AT","AU","AW","AZ","BA","BB","BD","BE","BF","BG","BH","BI","BJ","BM","BN","BO","BR","BS","BT","BV","BW","BY","BZ","CA","CC","CD","CF","CG","CH","CI","CK","CL","CM","CN","CO","CR","CU","CV","CX","CY","CZ","DE","DJ","DK","DM","DO","DZ",
"EC","EE","EG","EH","ER","ES","ET","FI","FJ","FK","FM","FO","FR","FX","GA","GB","GD","GE","GF","GH","GI","GL","GM","GN","GP","GQ","GR","GS","GT","GU","GW","GY","HK","HM","HN","HR","HT","HU","ID","IE","IL","IN","IO","IQ","IR","IS","IT","JM","JO","JP","KE","KG","KH","KI","KM","KN","KP","KR","KW","KY","KZ",
"LA","LB","LC","LI","LK","LR","LS","LT","LU","LV","LY","MA","MC","MD","MG","MH","MK","ML","MM","MN","MO","MP","MQ","MR","MS","MT","MU","MV","MW","MX","MY","MZ","NA","NC","NE","NF","NG","NI","NL","NO","NP","NR","NU","NZ","OM","PA","PE","PF","PG","PH","PK","PL","PM","PN","PR","PS","PT","PW","PY","QA",
"RE","RO","RU","RW","SA","SB","SC","SD","SE","SG","SH","SI","SJ","SK","SL","SM","SN","SO","SR","ST","SV","SY","SZ","TC","TD","TF","TG","TH","TJ","TK","TM","TN","TO","TL","TR","TT","TV","TW","TZ","UA","UG","UM","US","UY","UZ","VA","VC","VE","VG","VI","VN","VU","WF","WS","YE","YT","RS","ZA","ZM","ME","ZW","A1","A2",
"O1","AX","GG","IM","JE","BL","MF"
};
private static string[] countryName = 
{"Unknown","Asia/Pacific Region","Europe","Andorra","United Arab Emirates","Afghanistan","Antigua and Barbuda","Anguilla","Albania","Armenia","Netherlands Antilles","Angola","Antarctica","Argentina","American Samoa","Austria","Australia","Aruba","Azerbaijan","Bosnia and Herzegovina","Barbados","Bangladesh","Belgium",
"Burkina Faso","Bulgaria","Bahrain","Burundi","Benin","Bermuda","Brunei Darussalam","Bolivia","Brazil","Bahamas","Bhutan","Bouvet Island","Botswana","Belarus","Belize","Canada","Cocos (Keeling) Islands","Congo, The Democratic Republic of the","Central African Republic","Congo","Switzerland","Cote D'Ivoire",
"Cook Islands","Chile","Cameroon","China","Colombia","Costa Rica","Cuba","Cape Verde","Christmas Island","Cyprus","Czech Republic","Germany","Djibouti","Denmark","Dominica","Dominican Republic","Algeria","Ecuador","Estonia","Egypt","Western Sahara","Eritrea","Spain","Ethiopia","Finland","Fiji","Falkland Islands (Malvinas)",
"Micronesia, Federated States of","Faroe Islands","France","France, Metropolitan","Gabon","United Kingdom","Grenada","Georgia","French Guiana","Ghana","Gibraltar","Greenland","Gambia","Guinea","Guadeloupe","Equatorial Guinea","Greece","South Georgia and the South Sandwich Islands","Guatemala","Guam","Guinea-Bissau","Guyana",
"Hong Kong","Heard Island and McDonald Islands","Honduras","Croatia","Haiti","Hungary","Indonesia","Ireland","Israel","India","British Indian Ocean Territory","Iraq","Iran, Islamic Republic of","Iceland","Italy","Jamaica","Jordan","Japan","Kenya","Kyrgyzstan","Cambodia","Kiribati","Comoros","Saint Kitts and Nevis",
"Korea, Democratic People's Republic of","Korea, Republic of","Kuwait","Cayman Islands","Kazakstan","Lao People's Democratic Republic","Lebanon","Saint Lucia","Liechtenstein","Sri Lanka","Liberia","Lesotho","Lithuania","Luxembourg","Latvia","Libyan Arab Jamahiriya","Morocco","Monaco","Moldova, Republic of","Madagascar",
"Marshall Islands","Macedonia","Mali","Myanmar","Mongolia","Macau","Northern Mariana Islands","Martinique","Mauritania","Montserrat","Malta","Mauritius","Maldives","Malawi","Mexico","Malaysia","Mozambique","Namibia","New Caledonia","Niger","Norfolk Island","Nigeria","Nicaragua","Netherlands",
"Norway","Nepal","Nauru","Niue","New Zealand","Oman","Panama","Peru","French Polynesia","Papua New Guinea","Philippines","Pakistan","Poland","Saint Pierre and Miquelon","Pitcairn Islands","Puerto Rico","Palestinian Territory","Portugal","Palau","Paraguay","Qatar","Reunion","Romania","Russian Federation","Rwanda","Saudi Arabia",
"Solomon Islands","Seychelles","Sudan","Sweden","Singapore","Saint Helena","Slovenia","Svalbard and Jan Mayen","Slovakia","Sierra Leone","San Marino","Senegal","Somalia","Suriname","Sao Tome and Principe","El Salvador","Syrian Arab Republic","Swaziland","Turks and Caicos Islands","Chad","French Southern Territories","Togo",
"Thailand","Tajikistan","Tokelau","Turkmenistan","Tunisia","Tonga","Timor-Leste","Turkey","Trinidad and Tobago","Tuvalu","Taiwan","Tanzania, United Republic of","Ukraine","Uganda","United States Minor Outlying Islands","United States","Uruguay","Uzbekistan","Holy See (Vatican City State)","Saint Vincent and the Grenadines",
"Venezuela","Virgin Islands, British","Virgin Islands, U.S.","Vietnam","Vanuatu","Wallis and Futuna","Samoa","Yemen","Mayotte","Serbia","South Africa","Zambia","Montenegro","Zimbabwe","Anonymous Proxy","Satellite Provider",
"Other","Aland Islands","Guernsey","Isle of Man","Jersey","Saint Barthelemy","Saint Martin"};
 
 
public CountryLookup(string fileName)
{
try
{
fileInput = new FileStream(fileName, FileMode.Open, FileAccess.Read);
}
catch (FileNotFoundException e)
{
throw new IOException("File " + fileName + " not found.");
}
}
 
public string lookupCountryCode(string str)
{
IPAddress addr;
try
{
addr = IPAddress.Parse(str);
}
catch (FormatException e)
{
return "-";
}
return lookupCountryCode(addr);
}
 
private long addrToNum(IPAddress addr)
{
long ipnum = 0;
byte[] b = addr.GetAddressBytes();
for (int i = 0; i < 4; ++i)
{
long y = b[i];
if (y < 0)
{
y += 256;
}
ipnum += y << ((3 - i) * 8);
}
//HttpContext.Current.Response.Write(ipnum);
return ipnum;
}
 
public string lookupCountryCode(IPAddress addr)
{
return (countryCode[(int)seekCountry(0, addrToNum(addr), 31)]);
}
 
public string lookupCountryName(string str)
{
IPAddress addr;
try
{
addr = IPAddress.Parse(str);
}
catch (FormatException e)
{
return "Unknown";
}
return lookupCountryName(addr);
}
 
public string lookupCountryName(IPAddress addr)
{
return (countryName[(int)seekCountry(0, addrToNum(addr), 31)]);
}
 
private long seekCountry(long offset, long ipnum, int depth)
{
byte[] buf = new byte[6];
long[] x = new long[2];
if (depth < 0)
{
//HttpContext.Current.Response.Write("Error seeking country.");
return 0; // N/A
}
try
{
fileInput.Seek(6 * offset, 0);
fileInput.Read(buf, 0, 6);
}
catch (IOException e)
{
throw new IOException("IO Exception");
}
for (int i = 0; i < 2; i++)
{
x[i] = 0;
for (int j = 0; j < 3; j++)
{
int y = buf[i * 3 + j];
if (y < 0)
{
y += 256;
}
x[i] += (y << (j * 8));
}
}
 
if ((ipnum & (1 << depth)) > 0)
{
if (x[1] >= COUNTRY_BEGIN)
{
return x[1] - COUNTRY_BEGIN;
}
return seekCountry(x[1], ipnum, depth - 1);
}
else
{
if (x[0] >= COUNTRY_BEGIN)
{
return x[0] - COUNTRY_BEGIN;
}
return seekCountry(x[0], ipnum, depth - 1);
}
}
 
public void Dispose()
{
if (_close && fileInput != null)
{
fileInput.Close();
fileInput = null;
}
}
}


4)
Now Upload to a server and call it as

Hence Run it...

Finally you got the solution.,.....



 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend