How can I incorporate a web counter (page hit) in ASP.Net to track the number of visitors in my site?

Recommended Answers

All 8 Replies

Why don't you track it with statistics?
Anyway, if you really want it, you can use global.asax file for that.

In Application_Start and Application_End event of global.asax set something like this:

Sub Application_Start(ByVal sender As Object, ByVal e As EventArgs)
        Application("Online") = 0
    End Sub
    
    Sub Application_End(ByVal sender As Object, ByVal e As EventArgs)
        Application("Online") = 0
    End Sub

Then, in Session events use this:

Sub Session_Start(ByVal sender As Object, ByVal e As EventArgs)
        Application("Online") += 1
    End Sub

    Sub Session_End(ByVal sender As Object, ByVal e As EventArgs)
        Application("Online") -= 1
    End Sub

Note: Set sessionstate mode to InProc in the web.config file.

To show it just put this in page:

Online users: <%=Application("Online")%>

Hope this helps, I haven't tested it.

I have just now realized that you need web count, not number of online users. :D
Well just add +1 in Session_Start event and store sum in text file.

if you want to see number of visits to individual pages,on page load event of each page, you can run insert statement against a database, include url of the page and ip of the user as columns in case you can group the results by these columns later

hi ebabes,
I think you needed website counter which simply tells number of times website has been visited rather than individual page visit counter. So, you have 2 alternatives to store Visit Count either in to Text File or in to database. I seen one article based on text file, just check the following link.

http://imar.spaanjaars.com/QuickDocID.aspx?QUICKDOC=227

http://www.daniweb.com/forums/thread6566.html

Hope this will help you. If problem persist then feel free to share with me.
Thanks & Regards
Dilip Kumar Vishwakarma
Programmer
.Net Consulting

Yes, you can store them within a database or within text, or even within XML.

For research on my websites to see which ones are popular, in which directories, etc. I store all my information in XML files, and upload them once every 15 minutes to the database server. It prevents an unnecessary hit to the database of thousands of times (which can definitely slow your site). I would recommend just storing page hits in an xml file, then upload the information from the xml file to your database server. Then your entire visitor count can be all page hits combined (a SUM command would work). This way it also lets you know which pages are receiving the majority of the traffic.

Its not much work to do, and I believe it to be highly recommended

Thanks buddy! Its a big help...

Yes, you can store them within a database or within text, or even within XML.

For research on my websites to see which ones are popular, in which directories, etc. I store all my information in XML files, and upload them once every 15 minutes to the database server. It prevents an unnecessary hit to the database of thousands of times (which can definitely slow your site). I would recommend just storing page hits in an xml file, then upload the information from the xml file to your database server. Then your entire visitor count can be all page hits combined (a SUM command would work). This way it also lets you know which pages are receiving the majority of the traffic.

Its not much work to do, and I believe it to be highly recommended

Do you have to read the entire xml file(load xml) to append new record to it? if you have to read the entire document then it may cost more than inserting single record to the database. Is there any simple append to xml method in c#

Not that I know of serk. Hey, can you tell me how to call an onSelectedIndexChanged event from Page_Load? Trying to help someone but I can't seem to get it right.

The reason I say I would do XML is because most of my sites are heavy on database interaction. This way the server does more processing than the SQL server, which is shared among many. Most of the time the SQL server is backed up while the web server is free and clear. That's why I use XML ^^

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.