Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 23rd, 2007, 08:12 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default Creating Web Counter Via Database

I have been experimenting with creating a web counter. I created a table (I'm using SQLServer 2005) that simply contains an integer.

Right now, I increment this on the page as:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            KnightAlertBox.Visible = "true"
            WebCounter.UpdateWebCounter()
        End If
End Sub

Where WebCounter is a function I wrote to simply increment the integer value in the table.

The problem, obviously, is that anytime I click off the default.aspx page and go back, it will increment it. Is there a different event that just triggers once per "site" visit?

Suggestions?

Thanks, in advance,
Rob



 
Old May 23rd, 2007, 08:19 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

Use your Global.asax file and the Session_OnStart event; place your code there. Obviously, this doesn't actually count the number of visitors to your website, it counts the number of sessions you have handled but that is going to be the best you can do without something like WebTrends to tell you unique visitors, etc. (Of course you could just write an app to parse your IIS logs to get that data if you really want it)

hth.



================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old May 23rd, 2007, 08:36 PM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

The global.asax file is an optional file used by the runtime that contains a series of events related to your web application as a whole.

Somethign like this will work


<script language="vb" runat="server">
Sub Session_OnStart(sender as Object, e as EventArgs)
    WebCounter.UpdateWebCounter()
End Sub
</script>

Place it in the root of your web application. There are other events that can be in a global file but for the sake of simplicity this will do for you.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========
 
Old May 23rd, 2007, 08:38 PM
Friend of Wrox
 
Join Date: Jul 2006
Posts: 238
Thanks: 0
Thanked 2 Times in 2 Posts
Send a message via MSN to rsearing
Default

UPDATE: Thanks for answering the previous question--I actually deleted it--as I did a bit of research and had it answered--but THANK YOU..only thing left is below:

Can someone help explain what the lifecycle of the session? Are you saying that a session could be servicing two users at the same time, thus not capturing the true amount of users? If so, how long would the "session" stay active until a new one would be generated that I would capture?

I'm quite the novice here, and not really understanding lifecycles of events.

What is the difference between session start and application start?

Thanks SO MUCH!
Rob
 
Old May 24th, 2007, 07:05 AM
Wrox Author
 
Join Date: Oct 2005
Posts: 4,104
Thanks: 1
Thanked 64 Times in 64 Posts
Send a message via AIM to dparsons
Default

No, 1 session will not handle 2 different users, it is a 1 to 1 relationship. Here is what is gong to happen by placing your code in the Session_OnStart:

A user will come to your site, and a session is created for them, thus calling your code to increment your code.

Now, if a user is idle at your site for 15 minutes (This is the default timeout for sessions but you can change it), their session will timeout and die; when they make another request to the server AFTER their session has expired, a new session will be created and will call your code in the Session_OnStart event.

So, as you can see, you code is going to really increment how many sessions you application serves and not necessarily how many users have come to your site. (In the above example, the user has incremented the counter twice but only visited your site once.)

By Session Lifecycle I think you mean to say Page Life Cycle and you can read about that here:
http://msdn2.microsoft.com/en-us/library/ms178472.aspx
A session's life is present as long as the user continues to do *something* and does not become idle for a period of time that exceeds the session time out.

Application_Start fires only 1 time every time the application is started. What I mean by this is that if you bounce your WebServer and restart it, Application_Start will fire once a resource has been requested from your application and will not fire again until the application is stopped through IIS or the server is restarted as a whole.

Session_OnStart will fire every time a new user requests resources from your application.

hth.

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
================================================== =========
Technical Editor for: Professional Search Engine Optimization with ASP.NET
http://www.wiley.com/WileyCDA/WileyT...470131470.html
================================================== =========
Why can't Programmers, program??
http://www.codinghorror.com/blog/archives/000781.html
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating Web Custom Controls Lerik C# 2005 0 January 6th, 2006 04:55 AM
Problem in creating web setup akhilvira ASP.NET 1.0 and 1.1 Basics 2 January 3rd, 2005 05:04 AM
Creating Web Service tact_259 .NET Web Services 0 April 5th, 2004 06:19 AM
couldn't creating a new Web project kemin VS.NET 2002/2003 2 March 27th, 2004 11:20 PM
web counter kiky Beginning PHP 2 February 23rd, 2004 08:39 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.