C# .NET - Hit counter - Asked By Dev M on 30-Sep-09 01:45 PM

how to add hit counter each web page?

re - Web Star replied to Dev M on 30-Sep-09 01:58 PM

We will create a Visual C# web application and add a web page in the new project. This web page will hold the core functionality of managing the hit counter in the text file and returning a graphic image to render the counter value.

This web page does not contain any server controls. In the Page_Load event, add the following code.


The above code invokes methods to get the counter value and render the image for the counter value.

Let’s take a look at the code that fetches the counter value.


The above function just reads the value from a text file. If the text file does not exist, the default value of 0 is used. The counter is incremented and the new value is saved in the same text file for the next invocation. The hit counter value is then returned to the calling function.

The DrawCounter function uses GDI+ classes from the .Net Framework to render an image containing the counter value. Let’s take a look at this function.


Now we have a web page that draws a custom gif image containing the hit counter value. Let’s see how to use this functionality in our application web pages.

Hit counter - DL M replied to Dev M on 30-Sep-09 02:13 PM

This code can be copied and pasted in the footer area of any .aspx file in your website.

The first thing you'll want to do is create an empty text file, call it counter.txt and save it to your root directory. The next step is even easier, copy and paste the code below into your .aspx file and save it. Be sure to import the system.IO class into your page something like this

<%@ import namespace="System.IO" %>

< script runat="server" >
public string counter()
{

StreamReader re = File.OpenText(Server.MapPath("counter.txt"));

string input = null;

string mycounter = "";

while ((input = re.ReadLine()) != null)

{

mycounter = mycounter + input;

}

re.Close();

int myInt = int.Parse(mycounter);

myInt = myInt + 1;

TextWriter tw = new StreamWriter(Server.MapPath("counter.txt"));

tw.WriteLine(Convert.ToString(myInt));

tw.Close();

re = File.OpenText(Server.MapPath("counter.txt"));
input = null;
mycounter = "";

while ((input = re.ReadLine()) != null)

{

mycounter = mycounter + input;

}
re.Close();

return mycounter;

}

< /script >

'copy this code to the bottom of your .aspx page.

< % Response.Write(counter() + " visited");% >

A brief description of what is going on in this code goes as follows:

a. create a method called counter

b. Call the StreamReader class from the system.IO library and read your text file

c. Store the value of the text file in a variable

d. Close the StreamReader

e. Open the StreamWriter

f. Add 1 to the variable holding the value from the text file

g. Write the new incremented by 1 value to the text file

h. Close the StreamWriter

This last line

<% Response.Write(counter() + " visited");%>

 is the line that calls the method when someone visits your homepage for instance. You can put all of the code at the bottom of the page, or if you are a little more experienced with c# you could place it in a "code-behind" file or an "include" of class methods to keep the code a bit more clean.

also more example

http://forums.asp.net/t/1423530.aspx

RE - Ravenet Rasaiyah replied to Dev M on 30-Sep-09 08:51 PM

Hi

It's to develop, here is complete article about this

http://archive.devnewz.com/2004/0127.html

thank you
Hit counter - mv ark replied to Dev M on 30-Sep-09 09:33 PM
The simplest way to track visitors is to use either of these services  -
STATCOUNTER - http://statcounter.com/ or
Google Analytics - http://www.google.com/analytics/

You will need to insert some Javascript they provide on the web pages that you want to track.

The advantage of this method is that you save on your resources
Vasanthakumar D replied to Dev M on 30-Sep-09 11:31 PM

Hi,

you can use three technologies for this...

1. Application variable

2. Storing counter in Database

3. Storing counter in files

storing counter in database will be the best method.

to store in application variable

Application["Counter"] = 1;

but this will be cleard when you restrat the IIS or your website.


Thanks you - Dev M replied to DL M on 01-Oct-09 12:52 AM

this code usefull thnaks also all guys

You can refer this links for Hit Counter - Vishal Chourasiya replied to Dev M on 01-Oct-09 01:42 AM

http://support.microsoft.com/kb/825532

http://www.codeproject.com/KB/custom-controls/ewcounter.aspx

http://www.developerfusion.com/code/3734/aspnet-graphical-page-hit-counter/