Active Server Pager (Dynamic Server Home, Abbreviation ASP), by reading and writing the server's file, combined with the Script language (VBScript or JScript) and HTML code, easy to implement page counter function. There are currently popular ASP textbooks and the ASP tutorials on the network talk about the design issues of the ASP counter, but they are too simple, such as how to implement how the counter script and the separation of the counter script and the main page, and the implementation of the image counter. Let's make an experience of NT Web sites in the author, and instance step-by-step is to talk about the design of the ASP counter. I hope to give ASP's beginners and netizens who are interested in ASP Web.
(1) Simple counter ASP contains five built-in "Active Server Components", namely Database Access Component, File Access Component, Ad Rotator Component (Advertising Broadcast Merical components), Brower Capabilities Component, Content Linking Component (Content Link Components). The counter we want to design below is achieved by reading and writing server files by the File Access Component. Algorithm ideas are: In the server side, a text (ASCII) file is stored, and each time the page reads a value from the file, display it to the user, and adds the value to 1, write the added value back to file. The ASP statement written to a server count file and the description as follows: counfile = server.mappath ("File Name") ")") The 'Server Server Access Method Mappath (PATH) is the file where the store counter value is path to physical path SET FileObject = Server.CreateObject ( "Scripting.FileSystemObject") 'use CreateObject definition object FileSystemObject SET OutStream = Server.CreateTextFile (FileObject, True, False)' using FileSystemObject object to provide a method to produce a text file CreateTextFile, wherein The parameter "true" represents the original file, "false" indicates that the file is an ASCII type Outstream.writeline "data to be written" 'outstream.writeline to write a line of data from a line of server from a server file ASP syntax as follows : CounFile = Server.MapPath ( "used to store the counter value of the file name") SET FileObject = Server.CreateObject ( "Scripting.FileSystemObject") SET InStream = Server.OpenTextFile (FileObject, 1, false, false) 'using the object FileSystemObject Method OpenTextFile generates a text file, 'where parameter "true" indicates that the original file is overwritten, "false" indicates that the file is ASCII type "= instream.readline' where instream.readline is read from the file. One line of data is a counter example (SimpleCounter.asp) with the ASP implementation page counter function, I will annotate the statement in detail in the code. You can paste the following code into the page code you need to count. Of course, your server must support the ASP, and you have created a text file SimpleCounter.txt in the home page.
Simple ASP Counter SimpleCounter.asp Code and Note: <% countfile = server.mappath ("SimpleCounter.txt") 'file aspConter.txt is used to store digital text files, initial content is usually 0 set fileObject = Server.createObject ( "Scripting.FileSystemObject") Set Out = FileObject.OpenTextFile (CountFile, 1, FALSE, FALSE) counter = Out.ReadLine 'Out.Close counter value read file' file Close SET FileObject = Server.CreateObject ( "Scripting .FilesystemObject ") set out = fileObject.createteTextFile (Countfile, true, false) Application.lock 'method Application.lock Disable other user Change The value of the counter counter counter = Counter 1' counter increase 1 Out.writeLine (Counter) 'Write the new counter value to file Application.unlock' After using method Application.unlock, allow other users to change the value of the counter Response.write ("You are No.) response.write (" " ) Response.write (counter) The value of the counter is sent to the browser, and the red (RED) is displayed to the user response.write (" font>") Response.write ("Bit Accessor" out.Close 'Close file%>
(2) In practical applications that are separated from the page, the main page is separated from the counter programs, just as long as you add a reference code in the page you need to add a reference code, you can implement the page count. The free counter we use for use online is the case, but they are generally CGI. Here, we only need to modify the simple counter that we do with ASP, then add a JavaScript statement to the page, enable the counter features that are separated from the page. In this way, it is convenient to count the counter as the main page or for a particular page. Obviously, you need to simply change the file name of the storage counter value and the counter ASP source code file name to implement multiple counters. And separating the page counter txtcounter.asp Code: <% 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 correct the value of the counter in the page, call the VBScript function document.write out.close%> Add the following code to the page: