One way to do it:
Do you like to know how many users visited your site? Creating a Web counter is very easy thing to do
using ASP. The only thing you have to do is to use an application server variable called count, this
variable will have the value zero when your application is started for the first time, and every time anew
visitor will come to your site the count variable will be incremented, the increment section will be
written in the Session_OnStart in the Global.asa through three steps:
¨ Lock the application server so no more than one visitor will try to increase it at the same time.
¨ Increment the count variable by one.
¨ Unlock the variable.
And that’s all what you have to do to create a simple counter and here is the code.
First let us do the Global.asa section:
1<script language="VBScript" runat="Server">
2Sub Session_OnStart
3' Lock the Application object
4Application.Lock
5
6' Increment the count
7Application("count") = Application("count") + 1
8
9' Unlock the Application object
10Application.Unlock
11End Sub
12</script>
And now to show the result you just need to retrieve the count variable like this in your web page:
1@ language="vbscript"
1<html>
2<head>
3<title>Counter Example 1</title>
4</head>
5<body>
6<h1> You are the visitor number ```
7=Application("count")
8```</h1>
9</body>
10</html>
Another way to do it:
Ok that’s very nice, but what will happen if the application stops for any reason? You will lose all the
data stored in the application variables and that includes the count variable. To solve this problem I
will use another way, you need to store the value of the count variable with in a text file or database.
For me I prefer database so I will use an MS access database with a table of one field called count and
the field called counter of type NUMBER.
1
2' Declare the variables that will be used with our code
3Dim Connection, RS, ConStr, Counts
4
5' Create and open the database connection
6Set Connection=Server.Createobjec t("ADODB.Connection")
7ConStr=("DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=" & Server.MapPath ("counter.mdb"))
8Connection.Open ConStr
9
10' Create the record set and retrive the counter value
11Set RS = Connection.Execute("SELECT * FROM count")
12
13' Increase the counter value by one
14Counts=RS("counter")+1
15
16' Update the counter value in the database
17Set RS = Connection.Execute ("UPDATE count SET counter =" & Counts)
18Connection.Close
1<html>
2<head>
3<title>Counter Example 2</title>
4</head>
5<body>
6<h1>You are the visitor number ```
7=Counts
8```</h1>
9</body>
10</html>
What about Active users:
Some guys wants to know how many visitors are currently seeing the site, for that we will use another way,
we will create an application variable called active, and on each new session we will increase it by one
and when a session ends we will decrease it by one, and here is the code:
The Global.asa:
1<script language="VBScript" runat="Server">
2
3Sub Application_OnStart
4'Create the variable that will hold the number of active visitors
5Application("Active") = 0
6End Sub
7
8Sub Session_OnStart
9'Increase the counter by one
10Application.Lock
11Application("Active") = Application("Active") + 1
12Application.UnLock
13
14End Sub
15
16Sub Session_OnEnd
17' Decrease the c ounter
18Application.Lock
19Application("Active") = Application("Active") - 1
20Application.UnLock
21End Sub
22
23</script>
And to show the results just call the Application variable active in your web page just like this:
1= Application("Active")
I hope you can create your own counters from now on. Give it try and good luck.