Tuesday, March 13, 2012

Session Variables ~ Counting People On a Web site

Hi All,

I have two questeions:

I would like to try and count how many people are on a web site at one time.....

Can this be done in the global asax file with session variables ?

I have written an short statement below , will this work......?

Also when using session variables,, do I ned to destroy them on session finish? if so how would i do that....

rgs

Tony

<script runat="server"
Sub Application_Start(Sender As Object, E As EventArgs)

Application("HowMany") = Application("HowMany") +1

Application("AllowAdd") = True
Application("MaxTries") = 3

End Sub

Sub Application_End(Sender As Object, E As EventArgs)
' Code that runs on application shutdown
End Sub

Sub Application_Error(Sender As Object, E As EventArgs)
' Code that runs when an unhandled error occurs
End Sub

Sub Session_Start(Sender As Object, E As EventArgs)
Session("NumTries")=0
End Sub

Sub Session_End(Sender As Object, E As EventArgs)
' Code that runs when a session ends
End Sub

</scriptYou will need an application Variable..

Application("Hits") = 0;

tehn on Session Start you increase that variable:

Application("Hits") += 1;

Note: you might have to do some casting, I'm not sure how that is in VB.

HTH,
Covo
BTW, you can't use the session variables these variables expire with the session, the application variables only expire when the application is re-started.

If you want something more persistent, you will have to implement a method to store your hits in a database/file.

Covo
Hi,

I guess something like this:


if(Application["Count"] != null)
{
Application["Count"] = ConvertTo.Int32(Application["Count"]) + 1;
}

else
{ Application["Count"] = 1; }


Try something like this...


' when the application starts set the variable to 0
Sub Application_Start(Sender As Object, E As EventArgs)

Application("HowMany") = 0

End Sub

' when a new user visits the site, a new session opens... so, increment the variable
Sub Session_Start(Sender As Object, E As EventArgs)

Application("HowMany") = Application("HowMany") + 1

End Sub

' when their session expires, decrement the variable
Sub Session_End(Sender As Object, E As EventArgs)

Application("HowMany") = Application("HowMany") - 1

End Sub

0 comments:

Post a Comment