ASP programming BASICS (15th): Counters

Source: Internet
Author: User

This component is used to create one or more counters that track the number of visits to a webpage or website. You can create only one counter component, but this component can have more than one counter.

Once a counter is created, it continues until it is deleted.

Counters are not automatically added when an event, such as accessing a page. You must use the Set and Increment methods to manually Set or increase the counter.

The counter is not limited by the scope. Once a counter is created, any page on the site can retrieve and control its values.

1. Counters. Set & Counters. Get
Counters. Set is mainly used to Set the initial value of the count, and Counters. Get is used to extract the Count value.

1, counts_set.asp

<%
Set Counters = server. createobject ("MSWC. Counters ")
Counters. Set "cnbruce", 10
Counters. set "it365cn", 20
Counters. set "blueidea", 30
%>
<% = Counters. Get ("cnbruce") %> total visits to cnbruce.com <br>
Total visits <% = Counters. Get ("it365cn") %> to the it365cn.com website <br>
Total visits <% = Counters. Get ("blueidea") %> to blueidea.com <br>

The usage is simple. First,"Counters = server. createobject ("MSWC. Counters ")"Indicates a Counters component connection is established;Counters. Set "cnbruce", 10Indicates that the initial value "cnbruce" is counted as 10;Counters. Get ("cnbruce ")It indicates that the value "cnbruce" is extracted.

Ii. Counters. Increment
The function of this method is to add one key at a time. In the above example, When you refresh the page, the records are not added, so use the Increment method now.

2, counts_inc.asp

<%
Set Counters = server. createobject ("MSWC. Counters ")
%>
Total <% = Counters. Increment ("cnbruce") %> visits to cnbruce.com <br>
Total visits <% = Counters. Increment ("it365cn") %> to it365cn.com <br>
Total visits <% = Counters. Increment ("blueidea") %> to blueidea.com <br>

The basic code is similar, mainly because the Counters. Increment Function accumulates 1.

A simple voting system can be built based on accumulation.

3. vote. asp

<%
Set Counters = server. createobject ("MSWC. Counters ")
Vote = Request. QueryString ("site ")
Counters. Increment (vote)
%>
Vote for your Favorite Site:
<Form name = "Voting for Sites" METHOD = "GET" ACTION = "vote. asp">
<Input type = "RADIO" NAME = "site" VALUE = "cnbruce"> cnbruce
<Input type = "RADIO" NAME = "site" VALUE = "it365cn"> it365cn
<Input type = "RADIO" NAME = "site" VALUE = "blueidea"> blueidea
<BR> <input type = "SUBMIT" VALUE = "Submit Vote">
</FORM>
Current Vote Tally: <BR>
Cnbruce: <% = Counters. Get ("cnbruce") %> <BR>
It365cn: <% = Counters. Get ("it365cn") %> <BR>
Blueidea: <% = Counters. Get ("blueidea") %> <BR>

The function is to select a single-choice button and submit it. The corresponding counter value is Counters. Increment

Iii. Counters. Remove
This object is used to clear the records.

4. remove. asp

<%
Set Counters = server. createobject ("MSWC. Counters ")
Vote = Request. QueryString ("site ")
Clear = Request. QueryString ("clear ")
Counters. Increment (vote)
If Not clear = "" Then
Counters. Remove ("cnbruce ")
Counters. Remove ("it365cn ")
Counters. Remove ("blueidea ")
End If
%>
Vote for your Favorite Site:
<Form name = "Voting for Sites" METHOD = "GET" ACTION = "remove. asp">
<Input type = "RADIO" NAME = "site" VALUE = "cnbruce"> cnbruce
<Input type = "RADIO" NAME = "site" VALUE = "it365cn"> it356cn
<Input type = "RADIO" NAME = "site" VALUE = "blueidea"> blueidea
<BR> <input type = "SUBMIT" VALUE = "Submit Vote">
</FORM>
Current Vote Tally: <BR>
Cnbruce: <% = Counters. Get ("cnbruce") %> <BR>
It365cn: <% = Counters. Get ("it365cn") %> <BR>
Blueidea: <% = Counters. Get ("blueidea") %>
<Form name = "Clear Counters" METHOD = "GET" ACTION = "remove. asp">
<BR> <input type = "SUBMIT" VALUE = "Clear the Counters" NAME = "clear">
</FORM>

Through today's contacts, we can conclude that a simple counting system already has two Methods: Using the Application object and using the Counters component. However, FSO is also used, or the database is needed.

When using ASP components, pay attention to the following:

When we use server components to create objects, there are generally two methods. The common method is to use the CreateObject () method of the SERVER object, as shown in the following code:
SET NextLink = Server. CreateObject ("MSWC. NextLink ")

This method is easy to use, but there is another way to save system resources. Because when you create an object using the Server. CreateObject () method, the object is immediately loaded, and the system allocates memory and other resources for the object.

The second method to create an OBJECT is to use the HTML <OBJECT> tag. The syntax is as follows:
<OBJECT Runat = Sever ID = NextLink ProgID = "MSWC. NextLink"> </OBJECT>

When you use the <OBJECT> tag to create an OBJECT, the OBJECT you want to create will not be loaded immediately, but will not be loaded until the method or attribute of this OBJECT is referenced for the first time. In this way, we will save some resources. Although not many, it is very helpful to save server resources when your system receives nearly one million clicks every day. (Pine)

As said: Our Counters can be stored in global. asa.

<Object runat = Server SCOPE = Application ID = Counter PROGID = "MSWC. Counters"> </OBJECT>

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.