I am a newbie and developing a website using ASP .Net 2.0 with C# 2005. I would like to add a facility to count the no. of visitors to my website. I have collected the basic informations to add this feature using Global.asax. I have made modifications to Web.config by adding the line "<sessionState mode="InProc" cookieless="false" timeout="60" />" under system.web section.

I am using a table to keep the count of visitors. But I don't know how to complete the task. My default Global.asax file came with different sections Application_Start, Application_End, Application_Error, Session_Start and Session_End. I have tried to extract the current value of the counter in the Application_Start section and store in a global variable. I would increment the counter in Session_Start and write the modified value to the table in Application_End.

I have tried to use public subroutines/functions. But where should I place those subroutines? I tried to add the subroutines in the Global.asax itself. But now I am getting errors, as I can not add reference to Data.SqlClient in Global.asax and I need references to SqlConnection, SqlCommand, SqlDataReader etc. to implement the features. Do I have to add Class files for each subroutines? Please guide me.

I would also like to implement tracking feature to my website and store the IP address, browser used, date and time of visit, screen resolution etc of my websites visitors. How can I do it?

Waiting for suggestions.

Lalit Kumar Barik

hi,

check this code.assign this in your global.asx file

Sub Application_OnStart
'initialize variable
Application("visitors_online") = 0
End Sub
Sub Application_OnEnd

End Sub

Sub Session_OnStart
Session.Timeout = 20 '20 minute timeout
Application.Lock
Application("visitors_online") = Application("visitors_online") + 1
Application.Unlock
End Sub
Sub Session_OnEnd
Application.Lock
Application("visitors_online") = Application("visitors_online") - 1
Application.Unlock
End Sub

On line users: <%=Application("visitors_online")%>

I want to store the counter in a table. I also want to store other details like IP address, browser used, etc. in a table so that I can generate reports from my website hits.

Lalit Kumar Barik

What the previous post gave you was a way to know how many people are online at any given moment, however, it is very unstable as you are not guaranteed to trigger Session_OnEnd, and if you look into it, it's normally not fired for beginners.

I would suggest, just like he stated above, put in the global.asax file a Session_OnStart method. In this method, you can tap your database and insert a row. Look up how to retrieve the IP info, and you can also retrieve everything about the user's browser and certain computer information as well.

However, I would suggest not using a database for this for every visitor. And if you decide to, use Asynchronous calls. You don't want your visitor to wait even 1 second before the page starts to load.

I would suggest writing this to a file, or XML. Then set up a trigger so that every hour, or 6 hours, you read through the XML and send the data to the database. Thus your visitors do not wait for anything. You can also set up the file IO writing to be Asynchronous as well. This is more of an advanced step, but will save you lots of trouble in the future.

This article has been dead for over six months. Start a new discussion instead.