943,639 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 3050
  • ASP.NET RSS
Mar 20th, 2009
0

Implementing a visitor counter

Expand Post »
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
Reputation Points: 10
Solved Threads: 0
Light Poster
Eager_Beever is offline Offline
45 posts
since Apr 2007
Mar 21st, 2009
0

Re: Implementing a visitor counter

hi,

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

ASP.NET Syntax (Toggle Plain Text)
  1. Sub Application_OnStart
  2. 'initialize variable
  3. Application("visitors_online") = 0
  4. End Sub
  5. Sub Application_OnEnd
  6.  
  7. End Sub
  8.  
  9. Sub Session_OnStart
  10. Session.Timeout = 20 '20 minute timeout
  11. Application.Lock
  12. Application("visitors_online") = Application("visitors_online") + 1
  13. Application.Unlock
  14. End Sub
  15. Sub Session_OnEnd
  16. Application.Lock
  17. Application("visitors_online") = Application("visitors_online") - 1
  18. Application.Unlock
  19. End Sub
  20.  
  21. On line users: <%=Application("visitors_online")%>
Reputation Points: 25
Solved Threads: 29
Posting Whiz
greeny_1984 is offline Offline
372 posts
since Apr 2007
Mar 21st, 2009
0

Re: Implementing a visitor counter

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
Reputation Points: 10
Solved Threads: 0
Light Poster
Eager_Beever is offline Offline
45 posts
since Apr 2007
Mar 23rd, 2009
0

Re: Implementing a visitor counter

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.
Reputation Points: 43
Solved Threads: 68
Veteran Poster
SheSaidImaPregy is offline Offline
1,080 posts
since Sep 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: Asp.net+Crystal report
Next Thread in ASP.NET Forum Timeline: TreeView Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC