Wrox Programmer Forums
|
Classic ASP Professional For advanced coder questions in ASP 3. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Professional 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 September 28th, 2004, 03:41 PM
Registered User
 
Join Date: Jun 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to FlashMan Send a message via MSN to FlashMan Send a message via Yahoo to FlashMan
Default counter keeps reseting

Hello!

I have created a counter that records number of visits and active users. The problem is that after a certain period of time the counter resets itsef to zero. None of my pages contain script that would reset the counter.

Here's the code:

<script language="VBScript" runat="server">

    Sub Application_OnStart
        Application("visits") = 0
        Application("active") = 0
        Application("activeHighest") = 0
        Application("activeTime") = 0
    End Sub

    Sub Session_OnStart
        Session.Timeout = 20
        Session("userClass") = 0
        Application.Lock
            Application("visits") = Application("visits") + 1
            Application("active") = Application("active") + 1
        Application.Unlock
    End Sub

    Sub Session_OnEnd
        Application.Lock
            Application("active") = Application("active") - 1
        Application.Unlock
    End Sub

    Sub Application_OnEnd
    End Sub
</script>

Please help me find a solution.
Thank you!

 
Old September 28th, 2004, 03:54 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

This can be caused by IIS restarting. Check out the IIS logs, and see if a restart of IIS happens at the same time your counters are reset.

This can be caused by IIS's built-in process recycling, and maybe even by anti-virus software marking the global.asa as changed when checking the file.

Cheers,

Imar
 
Old September 28th, 2004, 03:57 PM
Registered User
 
Join Date: Jun 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to FlashMan Send a message via MSN to FlashMan Send a message via Yahoo to FlashMan
Default

Thanks Imar!

Is there a more secure way of creating a counter?

 
Old September 28th, 2004, 04:14 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

You can write the counters to a persistent medium like a file or a database. Check out the following three articles (the last one demonstrates what you currently have, listing your findings as one of the problems, but it might be a good read anyway to get introduced to the concepts of hit counting):

Database:
http://Imar.Spaanjaars.Com/QuickDocID.aspx?QUICKDOC=165

Text File:
http://Imar.Spaanjaars.Com/QuickDocID.aspx?QUICKDOC=164

App Vars:
http://Imar.Spaanjaars.Com/QuickDocID.aspx?QUICKDOC=161

Cheers,

Imar
--------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Demise by Tricky (Track 10 from the album: Angels With Dirty Faces) What's This?
 
Old September 28th, 2004, 04:23 PM
Registered User
 
Join Date: Jun 2004
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via ICQ to FlashMan Send a message via MSN to FlashMan Send a message via Yahoo to FlashMan
Default

Many Thanks Imar!

I will try the method listed in article 164.

Using method described in article 165 does sound like a good idea, but i'm using Jet4 drivers,, so if I have many concurrent users, things will get pretty slow.

 
Old September 29th, 2004, 12:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

If you're having a lot of concurrent users, the text file isn't an option either. In that case, use a real database like SQL Server, Oracle, MySQL etc in combination with the techniques presented in article 165.

A text file will get you into problems much quicker than a (Access) database.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
 
Old September 29th, 2004, 01:46 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 596
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Imar as a side issue,
If the application is restarting through IIS or anti virus, would you expect the Application_OnEnd to fire?
Could you persist the current count in the application variable to a text file at that point and reinstate it at Application_OnStart?

======================================
They say, best men are molded out of faults,
And, for the most, become much more the better
For being a little bad.
======================================
 
Old September 30th, 2004, 12:56 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi Rod,

Sorry for my late response. I have been way too busy with work to be able to respond to any e-mail or posts here.

Anyway, yes, I think it will. Of IIS I am sure. The whole idea of Application_OnEnd is that it fires when IIS gets restarted or stopped. This means that if you shut down the machine, start and stop or restart IIS, Application_OnEnd will fire.

When you make a change to the Global.asa, both the Application and the existing Sessions are ended. For a quick test, create a Web site and add this code to the global.asa:
Code:
<script language="vbscript" runat="server">

    Const openForReading = 1 
    Const openForWriting = 2 
    Const openForAppending = 8 
    Dim fso, f 
    Dim txtStream, fileName
    fileName = "C:\Test.txt"

    Sub Application_OnEnd()
        Set fso = Server.CreateObject("Scripting.FileSystemObject")
        Set f = fso.GetFile(fileName)
        Set txtStream = f.OpenAsTextStream(openForAppending)
        ' Append lines to the file
        txtStream.WriteLine "App End"
        txtStream.Close
        Set txtStream = Nothing
        Set f = Nothing
        Set fso = Nothing
    End Sub

    Sub Session_OnEnd()
        Set fso = Server.CreateObject("Scripting.FileSystemObject")
        Set f = fso.GetFile(fileName)
        Set txtStream = f.OpenAsTextStream(openForAppending)
        ' Append lines to the file
        txtStream.WriteLine "Session End"
        txtStream.Close
        Set txtStream = Nothing
        Set f = Nothing
        Set fso = Nothing
    End Sub
</script>
Create an empty text document called C:\Test.txt (the code assumes it's present).
Next, create a simple page with this code:
Code:
<%
    If Session("Test") = "" Then
        Session("Test") = Now()
    End If
    Response.Write(Session("Test"))
%>
Run the page to start the session. Refresh the page a couple of times and you'll notice the time stays the same (because the session is still alive).
Next, open the global.asa file, add a line break at the top of the page (to make the page dirty) and save the file. Next, refresh the page in your browser again. You'll notice the time will change because the Session has ended.

Finally, open C:\Test.txt. You'll find both Session_OnEnd as Application_OnEnd has fired.

Now, all that is left to figure out is whether an Anti Virus program actually "dirties" the file global.asa, causing the behavior described above to occur. I'll leave this quest to the readers of this thread, and go back to work now ;)

Cheers,

Imar





Similar Threads
Thread Thread Starter Forum Replies Last Post
C# classes working for multiple forms w/ reseting Nathan Fleming C# 2005 1 September 11th, 2006 04:45 PM
Hit Counter Help! NeilS21 Classic ASP Databases 4 April 29th, 2005 06:59 AM
Counter tp194 Javascript 1 September 2nd, 2004 08:02 AM
counter Adam H-W Classic ASP Basics 15 August 15th, 2003 11:18 AM





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