Simple C# Page Hit Counter


by Benjamin Cortese - Date: 2007-01-07 - Word Count: 499 Share This!

If you have or are interested in developing your own website, there probably will come a time when you want to get a general idea of how many times your website is being visited. There are more elaborate ways to do this of course, some developers refer you to reading the server logs, others will suggest using a third party product and implementing it's code within your own website.

For the rest of you who would rather just get a general idea if your personal website is getting roughly 10 hits per day or 1000, this little code snippet will help get you on your way. I have developed elaborate site analysis applications that provide statistics on what pages are being viewed, how long the visitor remains at that specific page, what browser they are using, who referred them to the website to begin with as well as what their I.P address is and the list goes on. For the sake of this article though, we'll be using the k.i.s.s method.

The level of this code requires only a minimal amount of knowledge of the .NET environment, and of course your website should be hosted on a windows server with the .NET environment.

This code can be copied and pasted in the footer area of any .aspx file in your website. Generally, you would put it in the footer of the home page, or at least that's a good place to start.

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

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;

}

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

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
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.

Nothing fancy, but gives me an idea of how active the site has been recently. Hope this helps you in the same way that it has helped me.

Happy Coding!


Related Tags: net, counter, ben cortese, c#, asp.net

Ben Cortese is a developer and business analyst for the financial industry and enjoys developing personal websites.

Copyright 2006. Article can be reprinted as long as author credits are given and content remains unchanged and intact.

An example where I use these feeds can be seen in action here http://www.news-junkie.net. This is a personal site that I had developed a while ago that is a simple news aggregation website with a number of different topics. I found it fun to develop and good practice working with xml and CF. The information is constantly updated and visitors to my site see fresh new articles everyday, and all I have to do now that it is built, is keep the domain name active and maybe switch around a few of my affiliate links which hopefully provide a little income to pay for the hosting of the site.

Your Article Search Directory : Find in Articles

© The article above is copyrighted by it's author. You're allowed to distribute this work according to the Creative Commons Attribution-NoDerivs license.
 

Recent articles in this category:



Most viewed articles in this category: