In this example we will create a Global.asa file that counts the number of current visitors.

  • The Application_OnStart sets the Application variable "visitors" to 0 when the server starts
  • The Session_OnStart subroutine adds one to the variable "visitors" every time a new visitor arrives
  • The Session_OnEnd subroutine subtracts one from "visitors" each time this subroutine is triggered

The 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>

To display the number of current visitors in an ASP file:

<!DOCTYPE html>
<html>
<head>
</head>
<body>
<p>There are <%response.write(Application("visitors"))%> online now!</p>
</body>
</html>



Practice Excercise Practice now