Create a counter for a Web page in several ways

Source: Internet
Author: User
Tags count variables access database
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 your have to be to be to be application server variable called count, this
Variable'll have the value zero when your application are started for the "the", and every time anew
Visitor'll come to your site The count variable would be incremented, the increment section'll be
Written in the Session_OnStart Global.asa through three steps:
¨lock the application server so no more than one visitor would try to increase it at the same time.
¨increment the Count variable by one.
¨unlock the variable.
And that's all what your have to do to create a simple counter and this is the code.
The Global.asa section of the US do:

<script Language=vbscript runat=server>
Sub Session_OnStart
' Lock the Application object
Application.Lock

' Increment The Count
Application ("count") = Application ("Count") + 1

' Unlock the Application object
Application.UnLock
End Sub
</SCRIPT>


And now to show the result for you just need to retrieve the count variable like this in your Web page:
<%@ language= "VBScript"%>
<HTML>
<HEAD>
<title>counter Example 1</title>
</HEAD>
<BODY>
<H1> you are the visitor number <%=application ("Count")%></BODY>
</HTML>



Another Way to do it:

Ok that's very nice, but what would happen if the application for any stops? You'll lose all the
Data stored in the application variables and that includes the count variable. To solve this problem I
Would use another way, your need to store the value of the "the count variable with in a text file or database."
For me I prefer database I'll use an MS Access database with a table of one field called Count and
The field called Counter of type number.

<%
' Declare the variables that'll be used with our code
Dim Connection, RS, Constr, Counts

' Create and open ' database connection
Set connection=server.createobjec t ("ADODB. Connection ")
Constr= ("Driver={microsoft Access DRIVER (*.mdb)};" Dbq= "& Server.MapPath (" Counter.mdb "))
Connection.Open Constr

' Create the record set and retrive the counter value
Set RS = Connection.Execute ("SELECT * from Count")

' Increase the counter value by one
Counts=rs ("Counter") +1

' Update the counter value in the database
Set RS = Connection.Execute ("UPDATE count SET counter =" & Counts)
Connection.close
%>

<HTML>
<HEAD>
<title>counter Example 2</title>
</HEAD>
<BODY>
</BODY>
</HTML>



What about Active Users:

Some guys wants to know how many visitors are currently the site, for that we'll use seeing another,
We'll create a application variable called active, and on each new session we'll increase it by one
Session ends we'll decrease it by one, and this is the code:

The Global.asa:
<script language= "VBScript" runat= "Server" >

Sub Application_OnStart
' Create the variable that'll hold the number of active visitors
Application ("Active") = 0
End Sub

Sub Session_OnStart
' Increase the counter by one
Application.Lock
Application ("active") = Application ("active") + 1
Application.UnLock

End Sub

Sub Session_OnEnd
' Decrease the C ounter
Application.Lock
Application ("active") = Application ("active")-1
Application.UnLock
End Sub

</SCRIPT>


The results just call the application variable active in your Web page just as this:
<%= application ("Active")%>




I hope you can create a your own counters from the. Give it try and good luck.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.