Simple C# Page Hit Counter
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 ArticlesRecent articles in this category:
- Hosting Geeklog Sites----Leading To a Booming Online Community
The weblogs are increasingly viewed as a cheaper alternative of expensive content management system. -
-
-
-
-
- Take the Wheel - Drive Customers to your Website
So you own a small business and you have made the decision to build a simple website to gain interne - Joomla Revolution! How This Free Software Is Changing The Web?
Joomla is changing the web, there is no one better sentence to describe what this young FREE SOFTWAR - Spicing Up Your Blog with 3D Animated YouTube Playlists
People have blogs for different purposes, some people maintain a blog purely for commercial reasons, - Free Image Hosting Service
Image hosting is a part of most web hosting services, providing hosting solutions for images instead
Most viewed articles in this category:
- Beyond the Brand
After completing a "Who, What, Where, Why and How" exercise, there are a few more steps to preparing - Build A Better Website
Building a website isn't usually rocket science, but you have to do your homework. Just like buying - SharePoint 2007 New Feature Overview
Sharepoint 2007 is in Beta 2 now and is projected to be released at the beginning of the year but n - How To Install SharePoint 2007 Beta 2
Following are the detailed instructions on how to install SharePoint 2007 Beta 2 on a clean version - How To Accomplish Your Goals With A Web Site
Choosing a small business idea to reach your goals is not easy. You'll need a strong spirit and dete - The Best Web Sites Aren't Free
The best web sites aren't free. While you may be tempted to buy an HTML book and try to put a few we - URL Forwarding and Redirection
Webmasters and common Internet users have a constant need for URL forwarding and redirection. The r - Web Site Development with Ruby on Rails and Other Programs
Web site development is a very big business these days. Almost every company in the world is trying - Self Assessment: How Effective is Your Web Site?
1. Fonts are an important attribute of a Web site. It is essential that fonts are large and c - For Automated Sites - PHP and MySQL are A Perfect Match
You’ve decided to automate your web site. Now what? Here are some ideas to help you choose how