ASP counter Design Detailed-1

Source: Internet
Author: User
Tags add object count implement readline reference access
Counter | design | Details Active Server Pager (Dynamic Servers home page, abbreviated as ASP) can easily implement page counter functions by reading and writing server files, combining script language (VBScript or JScript) and HTML code. Now popular ASP textbooks and ASP tutorials on the network have talked about the design of ASP counters, but are too simple, such as not mention how to implement counter script and the separation of the main page and the implementation of the image counter. The following is the author for the organization of the experience of the NT Web site, give an example step-by-step talk about the design of ASP counters, I hope to give the ASP's Beginners and ASP Web programming interested in some of the netizens some inspiration.

   (a) Simple counter
ASP contains five built-in "Active Server Components" (ActiveX server components), which is database access component, file access component (Files Access component) , Ad Rotator Component (AD-carousel component), Brower capabilities component (Browser information component), Content Linking component (contents link component). The counter we want to design is to read and write the server file through the file access component. The algorithm idea is: In the server side with a text (ASCII) file to store the count value, whenever the page is accessed from the file read out the value, displayed to the user, and the value plus 1, the added value to write back to the file.
The ASP statements that write data to a server count file are described below:
Counfile=server.mappath ("FileName to hold counter value")
The server server access method MapPath (path) Converts the path of the file that holds the counter value to the physical path
SET fileobject=server.createobject ("Scripting.FileSystemObject")
' Use method createobject to define objects FileSystemObject
SET Outstream=server.createtextfile (Fileobject,true,false)
' Use object FileSystemObject provide method CreateTextFile generate text file, where parameter ' True ' means overwrite original file, ' False ' means file is ASCII type
Outstream.writeline "Data to write"
' Outstream.writeline writes a row of data to a file
The ASP syntax for reading data from a server file is as follows:
Counfile=server.mappath ("FileName to hold counter value")
SET fileobject=server.createobject ("Scripting.FileSystemObject")
SET Instream=server.opentextfile (Fileobject,1,false,false)
' Use object FileSystemObject to provide methods OpenTextFile generate text files,
' The argument ' True ' means overwrite the original file, ' False ' indicates that the file is of ASCII type
"Data to read" =instream.readline
' Where Instream.readline is a row of data read from a file
Here is a counter example (simplecounter.asp) that implements the page counter function with ASP, and I annotate the statement in detail in the code. You can paste the following code into the page code that you want to count. Of course, your server must support ASP, and you have created a text file Simplecounter.txt with content 0 in the same directory as the home page.
Simple ASP Counter simplecounter.asp code and comments:
<%
Countfile=server.mappath ("Simplecounter.txt")
' File Aspconter.txt is a text file for storing numbers, and the initial content is typically 0
Set fileobject=server.createobject ("Scripting.FileSystemObject")
Set Out=fileobject.opentextfile (Countfile,1,false,false)
Counter=out.readline
' Read the value in the counter file
Out.close
' Close the file
SET fileobject=server.createobject ("Scripting.FileSystemObject")
Set Out=fileobject.createtextfile (Countfile,true,false)
Application.Lock
' Method Application.Lock prevents other users from changing the value of the counter
counter= counter + 1
' Counter value increased by 1
Out.writeline (counter)
' Write the new counter value to the file
Application.UnLock
' Allow other users to change the value of the counter after using method Application.UnLock
Response.Write ("You are the first")
Response.Write ("<font color=red>")
Response.Write (counter)
' The value of the counter is passed to the browser, and the red color is displayed to the user
Response.Write ("</font>")
Response.Write ("Bit Visitor")
Out.close
' Close the file
%>

   (ii) counters separated from the page
In practice, the home page and the counter program are separate, so that the page count can be achieved by adding a reference code to the page that needs to be counted. This is what we often use for free counters on the Internet, but they are usually CGI. Here, we just slightly modify the previous we use ASP to do a simple counter, and then add a JavaScript statement in the page to reference it, we realize the separation of the counter function with the page. This makes it easy to count both as the main page counter or for a particular page. Obviously, you need to change the file name of the counter value and the counter ASP source code file name to implement multiple counters.
The counter txtcounter.asp code that separates from the page:
<%
Countfile=server.mappath ("Txtcounter.txt")
Set fileobject=server.createobject ("Scripting.FileSystemObject")
Set Out=fileobject.opentextfile (Countfile,1,false,false)
Counter=out.readline
Out.close
SET fileobject=server.createobject ("Scripting.FileSystemObject")
Set Out=fileobject.createtextfile (Countfile,true,false)
Application.Lock
counter= counter + 1
Out.writeline (counter)
Application.UnLock
Response.Write "Document.Write" ("&counter&") "
' In order to display the counter's value correctly on the page, call the VBScript function document.write
Out.close
%>
Add the following code to the page you want to count:
<p>
You are the first
<font color=red>
<script language= "JavaScript" src= "http://202.101.209.75/asptemp/counter/txtcounter.asp";>
Note the server and directory path where the ASP counters are located when referencing.
</script>
</font>
Guest
</p>

Related Article

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.