Introduction
Most of the web sites today using the hit count either to track the number of users (or) to display on the web sites to increase the popularity. There are many ways and each have pros and cons. In this article, I will try to show the different ways of counting and showing the number of users as follows:
- Using the Application variables(Global.asax)
- Using the text files
- Displaying the user count as a colorful image on the web sites
Using the Application variables (Global.asax )
Most of programmers, who think about counting number of users on the web site, first thing, that strikes in mind, are using the application variables to hold the user count. Because, it is simple to implement and can be accessed application wide. Let first see how to do this:
First open the global.asax file and go to session_start() function. Now add the following code:
-
-
protected void Session_Start(Object sender, EventArgs e)
-
{
-
Application[TOTALNUMBEROFUSERS] = Convert.ToInt32(Application[TOTALNUMBEROFUSERS]) + 1;
-
}
-
Use the application state variable TOTALNUMBEROFUSERS to access the count and display it to the users in your asp pages. This seems to be simple but, this approach has a major drawback, like, if the application restarts, the count will be lost because, the application variables are initialized again when the application starts.
Using the text files (Not lose User Count Between Application Restarts)
As discussed, storing user count on application variables is not a good option. We need some way of storing the user count, so even if the application restarts, we can access user count. The simple solution is using the text files to store the user count as follows:
-
-
Try
-
If not File.Exists(sFile) then
-
objWriter = File.CreateText(βcount.txtβ)
-
objWriter.Write("0")
-
objWriter.Close
-
End if
-
-
objReader = File.OpenText(sFile)
-
sCount = objReader.ReadToEnd()
-
objReader.Close
-
iCount = Cint(sCount)
-
iCount = iCount + 1
-
sCount = iCount.ToString
-
-
objWriter = File.CreateText(sFile)
-
objWriter.Write(sCount)
-
objWriter.Close
-
-
Catch Ex as Exception
-
Label1.width = New Unit(640)
-
sCount = "[Count could not be obtained due to the following error]: " & Ex.Message
-
-
Finally
-
-
HitCountLabel.Text = βThe total number of hits: β & sCount
-
-
End Try
-
Put the above code in the page_load function, so when the page is loaded the function executes each time and updates the user count.
Displaying the user count as a colorful image on the web sites
The above section describes how to save and retrieve the hit counter from the text file. By using the same hit counter, I will try to show to how to create a image with the number of users as follows:
[Continuation of the above example]
-
-
-
Dim bitmap as new Bitmap(200,150,
-
System.Drawing.Imaging.PixelFormat.Format32bppArgb)
-
Dim g as Graphics= Graphics.FromImage(bitmap)
-
Dim pen as Pen new Pen(Color.Yellow)
-
Dim rect as Rectangle new Rectangle(0,0,200,150)
-
Dim b as SolidBrush new SolidBrush(Color.DarkKhaki)
-
Dim blue as SolidBrush new SolidBrush(Color.Blue)
-
Dim counter as integer
-
Dim i as integer
-
-
counter =0
-
i=0
-
g.DrawRectangle(pen, rect)
-
g.FillRectangle(b, rect)
-
-
for i = 0 to sCount.Length - 1
-
-
g.DrawString(sCount[i],
-
new Font("Verdena", 10 + rand.Next(14, 18)),
-
blue, new PointF(10 + counter, 10))
-
-
next i
-
-
Response.ContentType = "image/gif"
-
bitmap.Save(Response.OutputStream,ImageFormat.Gif)
-
g.Dispose()
-
bitmap.Dispose()
-
Hi, this is Shiva Kumar. You can call me Shiva. I'm a developer like you, with 7 Years of experience on different technologies. Earlier I used to write articles for Computers Today. Now I started this web site to help each other ( or ) atleast contribute some thing from my end. I'm always a student, if you need any articles or help which is not there in this web site, please