Home
Products
Forums
Help
Publish Article
Go Freelance

How to find user location based on IP address or host address

A very common question web masters or web developers ask is how can they determine the country or location from where a user is visitng their web site. The only information you have available when a user to comes to your site is the host address or IP address of the user sent by browser in the request. So if you could just convert IP to location the job is done and you know geographic location of the user.

And what makes this work is a database that can map the IP address to the country or location. There are couple of free database availables that you can use. I have converted once such database to SQL Server and made available for everybody's use at the following location.

How to use it

The library exposes APIs that make the process very simple. Following code shows how you can detect country location of users of your web site. This is the same code that we have on IP2Country.Net library page.

void OnFindCoutry(object Source, EventArgs e)
{
        try
        {
            MSSqlIPProvider sqlProv = new MSSqlIPProvider("IPToCountry", "##my connection string for database##");
            IPRecord obRec = sqlProv.FindIPRecord(Request.UserHostAddress);
            if (null != obRec)
            {
                ctlCountryNameLabel.Text = obRec.CountryName;
                ctlCountryCodeLabel.Text = obRec.CountryCode;
                if (obRec.AssignedDate != DateTime.MinValue)
                {
                    ctlAssignedDateLabel.Text = obRec.AssignedDate.ToShortDateString();
                }

                ctlStartIPLabel.Text = obRec.FromIP;
                ctlEndIPLabel.Text = obRec.ToIP;
            }
            else
            {
                m_ctlStatusLabel.Visible = true;
                m_ctlStatusLabel.Text = "Could not resolve IP address " + ctlIPAddressTextBox.Text;
            }
        }
        catch
        {
            m_ctlStatusLabel.Visible = true;
            m_ctlStatusLabel.Text = "Could not resolve IP address " + ctlIPAddressTextBox.Text;
        }
 }							    
							    
Go Freelance
Home     About us     Contact us    Copyright    Privacy Policy    Return Policy    Advertisers
Copyright © Netomatix