ASP counter design detailed

zhaozj2021-02-17  48

ASP counter design detailed

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

The ASP contains five built-in "Active Server Components", which is Database Access Component, File Access Component, AD Rotator Component, Brower Capabilities, 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 is as follows:

Counfile = server.mAppath ("File Name") "" file name used to store counter values ​​") 'Server Server Access Method MAPPATH (PATH) is converted into a path where the file owner value is switched into a physical path set fileObject = Server.createObject (" scripting. FileSystemObject ") 'use CreateObject definition object FileSystemObjectSET OutStream = Server.CreateTextFile (FileObject, True, False)' using FileSystemObject object to provide a method CreateTextFile generating a text file, wherein the parameter" True "indicates overwrite the original file," False "indicates that the file is ASCII Type Outstream.writeline "Data to Write" 'Outstream.WriteLine writes a line of data to files

The ASP syntax read from a server file is 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) 'provided using 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. data

Below is a counter example (SimpleCounter.asp) with the ASP implementation page counter function, I comment 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 Notes:

<% Countfile = server.mappath ("SimpleCounter.txt") 'file aspConter.txt is a text file used to store numbers, and the initial content is usually 0set 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.CreateTextFile (CountFile , True, false Application.lock is prohibited by users to change the value of the counter counter counter = counter 1 'counter value Add 1WriteLine (counter)' Write the new counter value to file Application.unlock ' After using method Application.unlock, the value of other users can change the counter Response.Write ("You are No.) response.write (" ) response.write (counter) 'Transfer the value of the counter Go to the browser, display to the user response.write ("") response.close 'off file%>

(2) Counters with page separation

In practical applications, the home page is separated from the counter program, just simply add a reference code in a page that needs to be added. 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.

Counter txtcounter.asp code isolated:

<% 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 correctly display the value of the counter on the page, call the VBScript function document.writeout.close%> Add the following code to the page to count:

You are the server where the ASP counter is located when you are the server and Directory path. Enable guest

(3) Image counter with page separation

People's pursuit is never endless, maybe you need a more personality graphic digital counter instead of a simple text digital counter. No problem, now let's take a look at how to use the ASP to implement graphic counter function. To implement a graphic counter, the key is how to implement the data value in the counter file into the corresponding image representation. Because the number of decimers have a total of ten different numbers of 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9, we need to have ten corresponding images, and the image of the image is to display The number corresponds to, such as 0, the file name of the corresponding digital image is 0.GIF, 1 corresponds to 1.gif, ... (the image you can make tools such as Photoshop, or download it from the web). Here we need to use the VBScript function len (String | VarName), MID (String, Start [, Length]). The number of bits of the counter value can be obtained by the Len (Counter), and the number of the number of counter values ​​can be obtained from the MID (Counter, I, 1). We can use this value to call the corresponding digital image. With the for loop statement, we don't have to get the numbers on each bit of the counter and translate into a corresponding digital image, so we can implement the transformation of the text value to the image number. Below is an image counter instance written in ASP, because most of the code is analyzed, so only some statements are noteded in the code.

Image counter IMGCOUNTER.ASP code isolated:

<% @ language = "VBScript"%> <% DIM images (20) 'Defines an array to store 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) 'Get bit number for the counter value for i = 1 to countlenimages (i) = "< IMG SRC = "&" http://202.101.209.75/ASPTEMP/COUNTER/Images/ "&" / "& MID (Counter, i, 1) &" .gif> "is obtained by circulating statements The bit value corresponds to the display code (HTML) of the image, and put it in an array 'specifically used the actual server and directory path response.write "Document.write ('" & images (I) & " ); "'Call function Document.write output HTML code next to display digital image NEXTOUT.CLOSE%> Add the following code to the page to count:

You are the server and directory path where the ASP counter is located when you'll 's Script Language = "http://202.101.209.75/Asptemp/counter/imgcounter.asp"> //. Passenger

Note: The above ASP counters are passed under Windows NT Server 4.0 (Chinese) / IIS3.0. Any environment can be implemented in any of the following, Windows NT Server 4.0 / Iis3.0 above, Windows NT Workstation 4.0 / Microsoft Peer Web Service 3.0, Windows 95/98 / Microsoft Personal Web Server 1.0A or more

转载请注明原文地址:https://www.9cbs.com/read-29310.html

New Post(0)