The counter program of ASP instance is detailed

Source: Internet
Author: User
Tags array count implement readline reference string access
Program | counter |

The Active server Pager (Dynamic Servers home page, abbreviated as ASP) can easily implement page counter functionality by reading and writing server files, combining the 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")
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 to provide methods CreateTextFile produce a text file where the argument "True" indicates that the original file is overwritten, "False" indicates that the file is an 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)
Using object FileSystemObject to provide methods OpenTextFile produce text?
Where the argument "True" means overwrite the original file, "False" to indicate that the file is an 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:
$#@60;%
Countfile=server.mappath ("Simplecounter.txt")
File Aspconter.txt is a text file that is used to store 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
Reading the values in the counter file
Out.close
Close 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
The value of the counter increases by 1
Out.writeline (counter)
Writes the new counter value to the file
Application.UnLock
Allow other users to change the value of a counter after using method Application.UnLock
Response.Write ("You are the first")
Response.Write ("$#@60;font color=red$#@62;")
Response.Write (counter)
The value of the counter is passed to the browser, and the red color is displayed to the user
Response.Write ("$#@60;/FONT$#@62;")
Response.Write ("Bit Visitor")
Out.close
Close File
%$#@62;

  (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:
$#@60;%
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&") "
To display the value of the counter correctly on the page, call the VBScript function document.write
Out.close
%$#@62;
Add the following code to the page you want to count:
$#@60;p$#@62;
You are the first
$#@60;font color=red$#@62;
$#@60;script language= "JavaScript" src= "http://202.101.209.75/asptemp/counter/txtcounter.asp" $#@62;
Note the server and directory path where the ASP counters are located when referencing.
$#@60;/script$#@62;
$#@60;/font$#@62;
Guest
$#@60;/p$#@62;

   (iii) Image counter separated from the page

People's pursuit is endless, perhaps you need a more personalized graphics counter, rather than a simple text digital counter. No problem, now let's take a look at how ASP is used to achieve the function of graphics counters. In order to realize the graph counter, the key point is how to realize the conversion of the data value in the counter file to the corresponding image representation. Because the decimal number has a total of 10 different number of 0,1,2,3,4,5,6,7,8,9, we need to have 10 corresponding image, and the image of the file name to correspond with the number shown, such as 0 corresponding digital image of the file name is 0.gif,1 corresponding is 1.gif, ... (Images can be made using Photoshop tools, or downloaded from the web). Here we'll use the VBScript function len (string | varname), MID (String,start[,length]). The number of digits of the counter value can be obtained by Len (counter), and the number on the first bit of the counter value may be obtained by mid (counter,i,1), and we can use this value to invoke the corresponding digital image. With the FOR Loop statement, it is not difficult to get the numbers on each bit of counter values and convert them into corresponding digital images, so that we can convert the text values to the image numbers. The following is an example of an image counter written in ASP, because most of the code has been analyzed before, so only some of the statements are commented in the code.

Imgcounter.asp code for Image counter separated from page:
$#@60;% @language = "VBScript"%$#@62;
$#@60;%
Dim images (20)
Defines an array that holds statements that display each digit image
Countfile=server.mappath ("Imgcounter.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
Countlen=len (counter)
Gets the number of digits of the counter value
For I=1 to Countlen
Images (i) = "$#@60;img src=" & "http://202.101.209.75/asptemp/counter/images/" & "/" & Mid (counter,i,1) & ". gif$#@62;$#@60;/img$#@62;"
The Display code (HTML) of the numeric corresponding image on each bit is obtained by the Loop statement, and it exists in the array
Please note the actual server and directory path of image when using
Response.Write "Document.Write (" &images (i) & ");"
Call function document.write output display the HTML code of a digital image
Next
Out.close
%$#@62;

Add the following code to the page you want to count:
$#@60;p$#@62;
You are the first
$#@60;script language= "JavaScript" src= "http://202.101.209.75/asptemp/counter/imgcounter.asp" $#@62;
Note the server and directory path where the ASP counters are located when referencing.
$#@60;/script$#@62;
Guest
$#@60;/p$#@62;
Note: The above ASP counters are tested through Windows NT Server 4.0 (Chinese)/IIS3.0. Any of the following environments can perform asp:
One, Windows NT Server 4.0/iis3.0 above
Second, Windows NT WorkStation 4.0/microsoft Peer Web Service3.0 above
Third, Windows 95/98/microsoft Personal Web Server 1.0a above



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.