ASP counter design detailed (reprint)

Source: Internet
Author: User
Tags 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), the database access component, the File access component ( File 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.





write data to a server count file ASP statement and description as follows:





Counfile=server.mappath ("FileName to hold counter value")


' 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)


' uses object FileSystemObject to provide a method CreateTextFile produce a text file where the argument "True" indicates that the file is overwritten and "False" indicates that it 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)


' uses object FileSystemObject to provide methods OpenTextFile generate text files,


' where the parameter ' True ' means overwrite the original file, ' False ' means the file is an ASCII type


"data to read" =instream.readline


' where Instream.readline is a row of data read from a file





Below is a counter example (simplecounter.asp) that implements the page counter function using 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


' reads the value 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 prevent other users from changing the value of the counter


counter= counter + 1


' counter value increased by 1


out.writeline (counter)


' writes the new counter value to the file


Application.UnLock


' use method Application.UnLock to allow other users to change the value of the counter


Response.Write ("You are the first")


Response.Write ("<font color=red>")


Response.Write (counter)


' sends the value of the counter to the browser and displays it to the user in red color


Response.Write ("</font>")


Response.Write ("Bit visitor")


Out.close


' Close file


%>





(ii) Counter with page separation





actual application, the main page and counter program is separate, as long as the need to count the page to add a reference code to achieve page count. 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.





counter txtcounter.asp code with page separation:





<%


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 value of the counter correctly on the page, call the VBScript function document.write


Out.close


%>





Add the following code to the page to be counted:





<p>


you are the first


<font color=red>


<script language= "JavaScript" src=, ... (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. Here is an ASP-written image counter







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.