Hit counter on a web page
-
Hi, I am a new user to ASP.NET and VS.NET and I need some help. I am also a new user to CodeProject as well. I am trying to find a simple way to add a hit counter on a web page (ASP.NET and VB.NET) in VS.NET 2005. I found this example "Fast ASP.NET Hit Counter with Full Digit Graphic File Support" By JediBaron. I tried it but something goes wrong. I get this error on line 59:
Dim i As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(Request("src")))
I really cannot figure out what is wrong and how to correct the problem. Any help will be appreciated. Thanks.
-
Hi, I am a new user to ASP.NET and VS.NET and I need some help. I am also a new user to CodeProject as well. I am trying to find a simple way to add a hit counter on a web page (ASP.NET and VB.NET) in VS.NET 2005. I found this example "Fast ASP.NET Hit Counter with Full Digit Graphic File Support" By JediBaron. I tried it but something goes wrong. I get this error on line 59:
Dim i As System.Drawing.Image = System.Drawing.Image.FromFile(Server.MapPath(Request("src")))
I really cannot figure out what is wrong and how to correct the problem. Any help will be appreciated. Thanks.
This is off the top of my head. In your global.asax do this Sub Application_Start(ByVal sender AsObject, ByVal e As EventArgs) Application("Hits") = 0 EndSub Sub Session_Start(ByVal sender AsObject, ByVal e As EventArgs) 'Lock the Application for concurrency issues Application.Lock() 'Increment the counter Application("Hits") = Application("Hits") + 1 'Unlock the Application Application.UnLock() EndSub Than put this in your web form <%=application("Hits")%> times.
-
This is off the top of my head. In your global.asax do this Sub Application_Start(ByVal sender AsObject, ByVal e As EventArgs) Application("Hits") = 0 EndSub Sub Session_Start(ByVal sender AsObject, ByVal e As EventArgs) 'Lock the Application for concurrency issues Application.Lock() 'Increment the counter Application("Hits") = Application("Hits") + 1 'Unlock the Application Application.UnLock() EndSub Than put this in your web form <%=application("Hits")%> times.
Also he needs to store the hit count in permanent storage, so that even if the application gets restarted, the value Remains.. :-D
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Windows7 API Code Pack
Simplify Code Using NDepend
Basics of Bing Search API using .NET -
This is off the top of my head. In your global.asax do this Sub Application_Start(ByVal sender AsObject, ByVal e As EventArgs) Application("Hits") = 0 EndSub Sub Session_Start(ByVal sender AsObject, ByVal e As EventArgs) 'Lock the Application for concurrency issues Application.Lock() 'Increment the counter Application("Hits") = Application("Hits") + 1 'Unlock the Application Application.UnLock() EndSub Than put this in your web form <%=application("Hits")%> times.
Hi HarrissonB, This is a great start, but the counter gets reinitialized to 0 every time. There must be a simple way to store it somewhere and read the value from this data source. Now, I suppose there are many ways to store such a value, in a database (SQL Server, Access, etc.), in a text file or XLM file, and maybe some others. For a simple personal web site, what would be the best way (KISS method)? In a text file, would it be safe? Thanks.
-
Hi HarrissonB, This is a great start, but the counter gets reinitialized to 0 every time. There must be a simple way to store it somewhere and read the value from this data source. Now, I suppose there are many ways to store such a value, in a database (SQL Server, Access, etc.), in a text file or XLM file, and maybe some others. For a simple personal web site, what would be the best way (KISS method)? In a text file, would it be safe? Thanks.
-
It will only get set to 0 when the web server is restarted but when the page is reloaded.
Hi HarrisonB, The hit counter should never be reinitialized to 0, even when the server goes down. It should read its value from some data store where a start value could be set (<> 0) at the beginning. I think I can find some code where a xml file can be used. I will look it up and give you an answer as soon as I can. Thanks for your time.
-
Also he needs to store the hit count in permanent storage, so that even if the application gets restarted, the value Remains.. :-D
Abhishek Sur **Don't forget to click "Good Answer" if you like this Solution.
My Latest Articles-->** Windows7 API Code Pack
Simplify Code Using NDepend
Basics of Bing Search API using .NETHi Abhishek Sur, I do want to store the counter hits number in a data store, but cannot figure out how to do it. I have created a test database (DbTest) in SQL Server 2005 with a Param table and a simple column, NbHits. I can connect to it and display the number on a page. That is it. I would like my counter to be incremented every time a user visits the main page. I would also like to know which way is best for a simple personal web site. Do you know of some links where I could find some example? Thanks.