Tracking Clicks by IP Address Using C# and XML

This is a very basic article describing that tracing an IPAddress and getting the number of clicks for a particular control.  This gives the basic idea for counting the unique clicks for a control or for a page using XML.

This article contains only two pages. One is aspx and another one is XML file.

Problem Description:  I have an image control in my page. I need to display the number of clicks for particular user system. This means for the first time when I click on the control, it should display like "no of clicks is 1" and for the second time onwards the value will vary.

Technologies: C#.Net, VISUAL STUDIO 2008

Pages: noofclicks.aspx, noofclicks.aspx.cs, count.xml

Code for noofclicks.aspx:

<form id="form1" runat="server">

    <div>

        <asp:ImageButton ID="ImageButton1" runat="server" ImageUrl="~/middle.jpg" onclick="ImageButton1_Click" />

    </div>

</form>

Code behind for noofclicks.aspx page

using System;

using System.Collections;

using System.Configuration;

using System.Data;

using System.Linq;

using System.Web;

using System.Web.Security;

using System.Web.UI;

using System.Web.UI.HtmlControls;

using System.Web.UI.WebControls;

using System.Web.UI.WebControls.WebParts;

using System.Xml.Linq;

using System.Xml;

using System.Net;

using System.Data;

 

public partial class noofclicks : System.Web.UI.Page

{

    XmlDocument doc = new XmlDocument();

    int i;

    DataSet ds;

    int j;

 

    protected void Page_Load(object sender, EventArgs e)

    {

    }

 

    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)

    {

        //retrieve ip address

        string host = System.Net.Dns.GetHostName();

        string hostname = Dns.GetHostEntry(host).HostName;

        IPHostEntry ipEntry = Dns.GetHostEntry(host);

        IPAddress[] addr = ipEntry.AddressList;

 

        //xml

        doc.Load(Server.MapPath("~/count.xml"));

        XmlNodeList list = doc.SelectNodes("//NewDataset/section");

        foreach (XmlNode node in list)

        {

            //checking whether the ip address is there or not

            if (addr[0].ToString() == node.ChildNodes[1].InnerText)

            {

                i = Convert.ToInt32(node.ChildNodes[0].InnerText);

                i += 1;

                node.ChildNodes[0].InnerText = i.ToString();

                doc.Save(Server.MapPath("~/count.xml"));

                j = 1;

            }

            else

            {

                j = 0;

            }

        }

 

        if (j == 0)

        {

            ds = new DataSet();

            ds.ReadXml(Server.MapPath("~/count.xml"));

            DataRow row = ds.Tables[0].NewRow();

            row["count"] = "1";

            row["ip"] = addr[0].ToString();

            ds.Tables[0].Rows.Add(row);

            ds.WriteXml(Server.MapPath("~/count.xml"));

            Response.Write(" no of clicks for this image from ur ip address " + i + 1);

        }

        else

            Response.Write(" no of clicks for this image from ur ip address " + i);

    }

} 

count.xml File

<?xml version="1.0" standalone="yes"?>

<NewDataset>

  <section>

    <count>21</count>

    <ip>192.169.1.100</ip>

  </section>

  <section>

    <count>1</count>

    <ip>192.170.1.100</ip>

  </section>

  <section>

    <count>7</count>

    <ip>192.171.1.100</ip>

  </section>

  <section>

    <count>10</count>

    <ip>192.166.1.104</ip>

  </section>

  <section>

    <count>13</count>

    <ip>192.158.1.100</ip>

  </section>

  <section>

    <count>17</count>

    <ip>192.168.1.105</ip>

  </section>

  <section>

    <count>17</count>

    <ip>192.168.1.100</ip>

  </section>

</NewDataset>

This gives only a basic idea....

Enjoy the day buddies...!


Similar Articles