IT Advice and Code Snippets

    Paul Gough

 
home
 


Get IP and counrtry of your webpage visitors

It is sometimes useful to get a few details of your visitors, such as:

Your IP : 83.70.180.88  city : Dublin  region : Dublin  country : Ireland  country code : IE

Your browser name : IE  version : 7.0

Your language : en-us

Your cookies : None found

You can test it at http://www.goughit.com/samples/visitorinfo.aspx

So here is the code


Imports System.Data
Imports System.Net
Imports System.Xml

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim sIPAddress As String
        sIPAddress = Request.ServerVariables("HTTP_X_FORWARDED_FOR")
        If sIPAddress = "" Then sIPAddress = Request.ServerVariables("REMOTE_ADDR")
        LabIP.Text = sIPAddress

        Dim dt As DataTable = GetLocation(sIPAddress)
        If dt IsNot Nothing Then
            If dt.Rows.Count > 0 Then
                Labcity.Text = dt.Rows(0)("City").ToString()
                Labregion.Text = dt.Rows(0)("RegionName").ToString()
                Labcountry.Text = dt.Rows(0)("CountryName").ToString()
                Labcountrycode.Text = dt.Rows(0)("CountryCode").ToString()
            Else
            End If
        End If

        Dim bc As HttpBrowserCapabilities = Request.Browser
        Labbrowser.Text = bc.Browser
        Labversion.Text = bc.Version
        Labplatform.Text = bc.Platform

        Dim lanrequest As HttpRequest = HttpContext.Current.Request
        If lanrequest.UserLanguages Is Nothing Then
            Return
        End If
        Dim language As String = lanrequest.UserLanguages(0)
        If language IsNot Nothing Then
            If language.Length < 3 Then
                language = (language & "-") + language.ToUpper()
            End If
        End If
        Lablanguage.Text = language

        Dim output As New System.Text.StringBuilder()
        Dim aCookie As HttpCookie
        For i As Integer = 0 To Request.Cookies.Count - 1
            aCookie = Request.Cookies(i)
            output.Append("Cookie name = " & Server.HtmlEncode(aCookie.Name) & "<br />")
            output.Append("Cookie value = " & Server.HtmlEncode(aCookie.Value) & "<br /><br />")
        Next
        If output.Length > 0 Then
            Labcookies.Text = output.ToString()
        Else
            Labcookies.Text = "None found"
        End If
    End Sub
    Private Function GetLocation(ByVal ipaddress As String) As DataTable
        Dim rssReq As WebRequest = WebRequest.Create("http://freegeoip.appspot.com/xml/" & ipaddress)
        Dim px As New WebProxy("http://freegeoip.appspot.com/xml/" & ipaddress, True)
        rssReq.Proxy = px
        rssReq.Timeout = 2000
        Try
            Dim rep As WebResponse = rssReq.GetResponse()
            Dim xtr As New XmlTextReader(rep.GetResponseStream())
            Dim ds As New DataSet()
            ds.ReadXml(xtr)
            Return ds.Tables(0)
        Catch
            Return Nothing
        End Try
    End Function



A Simple Blog engine developed with .NET

Get IP and counrtry of your webpage visitors

Google maps Geocoder sample in vb aspx .net

Send an email to a mobile phone

Comments

Name