BRIEF INTRODUCTION:-
Every visitor i mean any user coming to your website/web application has an IP address. If you want to keep track of user ip address along with other details. IThe issue is when they're behind a proxy . So, here are the code snippets in ASP and .Net that first check for an IP addresses that's forwarded from behind a proxy, and if there's none then just get the IP address.
And here's the IP retriever with proxy detection in .Net (C#)
public string IpAddress()
{
string strIpAddress;
strIpAddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (strIpAddress == null)
{
strIpAddress = Request.ServerVariables["REMOTE_ADDR"];
}
return strIpAddress;
}
And here's the same IP retriever with proxy detection in .Net, but in
VB.Net (CODE BEHIND FILE OR FUNCTIONS)
Public Function IpAddress()
Dim strIpAddress As String
strIpAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
If strIpAddress = "" Then
strIpAddress = Request.ServerVariables("REMOTE_ADDR")
End If
IpAddress = strIpAddress
End Function
Now question is how you retieve all the details like
counry,region,region code ,latitite and logitute
Here is the complate code for getting all details about user
private string GetVisitor()
{//client connecting to a web server through an HTTP proxy or load balancer String ip = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"]; if (string.IsNullOrEmpty(ip))
{ip = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
}return ip;}
private DataTable GetLocation(string strIPAddress)
{WebRequest rssReq = WebRequest.Create("http://freegeoip.net/xml/" + strIPAddress); WebProxy px = new WebProxy("http://freegeoip.net/xml/" + strIPAddress, true); rssReq.Proxy = px; rssReq.Timeout = 2000; try
{WebResponse rep = rssReq.GetResponse(); XmlTextReader xtr = new XmlTextReader(rep.GetResponseStream()); DataSet ds = new DataSet(); ds.ReadXml(xtr); return ds.Tables[0];
}
catch
{return null;
}}
protected void btnGetIpaddress_Click(object sender, EventArgs e)
{try
{DataTable dt = new DataTable(); dt = GetLocation(GetVisitor()); if (dt.Rows.Count > 0) { dvIpaddressdetail.DataSource = dt; dvIpaddressdetail.DataBind();
}string ipaddress= dt.Rows[0].Field<string>(0);
}catch (Exception ex)
{throw ex;
}}
ASPX PAGE
<div class="middle-form" > <form name="form" autocomplete="off" runat="server"> <div class="row"> <h3 style="margin-left:20%;"> Lookup IP address and Domain location now with our free IP Locator.</h3> </div> <div class="row"> <asp:TextBox ID="txtipaddress" runat="server" Text="182.68.166.132" CssClass="text-holder"></asp:TextBox </div>
<div class="row">
<asp:Button ID="btnGetIpaddress" runat="server" Text="Lookup IP Address With IP Lookup" OnClick="btnGetIpaddress_Click" CssClass="btn btn-blue"/></div> <div class="row" style="margin-top:15px!important;"><br /> <asp:DetailsView ID="dvIpaddressdetail" runat="server" Width="100%" CellPadding="4" ForeColor="#333333" GridLines="None" HeaderText="IP ADDRESS DETAIL" BorderColor="#EEEEEE" BorderStyle="Solid"> <AlternatingRowStyle BackColor="White" /> <CommandRowStyle BackColor="#F9F9F9" Font-Bold="True" /> <FieldHeaderStyle BackColor="#F2F2F2" Font-Bold="True" /> <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" /> <HeaderStyle BackColor="#0099FF" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" /> <RowStyle BackColor="#FFFBD6" ForeColor="#333333" /> </asp:DetailsView> <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtipaddress" ErrorMessage="*"></asp:RequiredFieldValidator> <asp:HiddenField ID="txtlat" runat="server" ClientIDMode="Static" /> <asp:HiddenField ID="txtlon" runat="server" ClientIDMode="Static" /> </div> </form> </div>
See live demo of this http://shabirhakim.net/Tools/GetMyIPAddress.aspx