Results 1 to 3 of 3

Thread: counter

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Feb 2001
    Location
    Crossroads
    Posts
    3,046

    counter

    how do I implement a hit counter using asp.net?

  2. #2
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    to make something homemade, you have a few options...

    you could use a textfile to store your hit #'s...

    or if you want to store more advanced information (like referer urls or anything like that) you may want to consider an XML file or Database...

    But for a simple numeric count, it would be easy enough to just have a text file, every time the page loads, open the file, write the new number in the file, and then somewhere else on the page, display the new number..

    that is the basic idea

  3. #3
    Fanatic Member Redth's Avatar
    Join Date
    May 2001
    Location
    Ontario, Canada
    Posts
    551
    actually, even for a basic hit counter, i myself would go overboard and use an xml file... here's how i'd do it:

    Create New Dataset

    Fill DataSet with The hit counter XML File

    Change the Number in the Dataset to the new #

    Save the xml file from the Dataset


    it's actually quite simple to do in code

    VB Code:
    1. Dim dstHits As New DataSet()
    2.         Dim intHits As Integer
    3.  
    4.         'Read XML File into Dataset
    5.         dstHits.ReadXml("PageHits.xml")
    6.  
    7.         'Get the Current Number of Hits
    8.         intHits = Convert.ToInt32(dstHits.Tables(0).Rows(0)("HitCount"))
    9.  
    10.         'Increase the number of hits by one for this page hit
    11.         intHits += 1
    12.  
    13.         'Set the Dataset value to the new number of hits
    14.         dstHits.Tables(0).Rows(0)("HitCount") = intHits
    15.  
    16.         'Write the Dataset back into the XML File
    17.         dstHits.WriteXml("PageHits.xml")
    18.  
    19.         'Set the Label that is somewhere on the page with the number of hits
    20.         Label1.Text = "You are Visitor Number: " & intHits


    your XML File would look like this:
    VB Code:
    1. <?xml version="1.0" standalone="yes"?>
    2. <HitCounter>
    3.   <PageHits>
    4.     <HitCount>123</HitCount>
    5.   </PageHits>
    6. </HitCounter>

    That is a very basic example of a hit counter... you should be able to go from there

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width