Implementing a visitor counter

Please support our ASP.NET advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Implementing a visitor counter

 
0
  #1
Mar 20th, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 370
Reputation: greeny_1984 is an unknown quantity at this point 
Solved Threads: 29
greeny_1984's Avatar
greeny_1984 greeny_1984 is offline Offline
Posting Whiz

Re: Implementing a visitor counter

 
0
  #2
Mar 21st, 2009
hi,

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

  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")%>
If u r query is achieved,mark the thread as solved

Live and Let Live
Reply With Quote Quick reply to this message  
Join Date: Apr 2007
Posts: 45
Reputation: Eager_Beever is an unknown quantity at this point 
Solved Threads: 0
Eager_Beever Eager_Beever is offline Offline
Light Poster

Re: Implementing a visitor counter

 
0
  #3
Mar 21st, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 1,080
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Solved Threads: 68
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Implementing a visitor counter

 
0
  #4
Mar 23rd, 2009
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.
I answer pm's.
I answer questions.
I answer quickly.
I answer.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the ASP.NET Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC