7

I want to make my variable static or "global" - so the same effect as static in .NET; every session that accesses it gets the same result, and if one session modifies it it affects everyone else too.

How can I achieve this in Classic ASP?

Metro Smurf
  • 37,266
  • 20
  • 108
  • 140
joshcomley
  • 28,099
  • 24
  • 107
  • 147

2 Answers2

7

using a session variable

Session("myVariableName") = "my new value"

the scope will be the user...

if you want to wide the scope to ALL users that are in the website, then you use an Application variable

Application("myVariableName") = "my new value"

you can reset or handle this in the global.asa file as well

This is a common thing to do:

global.asa file:

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

Sub Application_OnStart
  Application("visitors") = 0
End Sub

Sub Session_OnStart
  Application.Lock
  Application("visitors") = Application("visitors") + 1
  Application.UnLock
End Sub

Sub Session_OnEnd
  Application.Lock
  Application("visitors") = Application("visitors") - 1
  Application.UnLock
End Sub

</script>

default.asp file:

<html>
 <head>
 </head>
 <body>
  <p>There are <%response.write(Application("visitors"))%> online now!</p>
 </body>
</html>
balexandre
  • 73,608
  • 45
  • 233
  • 342
  • 1
    No I don't want session variables - that is scoped per session! – joshcomley May 26 '09 at 09:34
  • 1
    if you want to wide up the scope to all users, use the Application variable as I specify in my answer :) this is how we accomplish "How many users" are in the website for example ;) – balexandre May 26 '09 at 09:35
7

If you want to have a variable that is accessible application wide, you can use the application object. Be sure to use Application.Lock/Unlock to prevent any problems.

Application.Lock
Application("MyVariable") = "SomeValue"
Application.Unlock
Jose Basilio
  • 50,714
  • 13
  • 121
  • 117