Recommended: ASP instance learning: function that randomly generates file names The following is the quoted content: <html> <meta http-equiv=Refresh content=2> <!-- Place this
Active Server Pager (dynamic server homepage, referred to as ASP), can easily realize the page counter function by reading and writing server files, combining script language (VBscript or Jscript) and html code. The popular ASP textbooks and ASP tutorials on the Internet have talked about the design issues of ASP counters, but they are all too simple, such as not mentioning how to separate the counter script and the main page and the implementation of the image counters. Below is the author's experience in creating NT web sites for units, and give examples to talk about the design of ASP counters step by step. I hope it can give some inspiration to beginners of ASP and netizens who are interested in ASP WEB programming.
(I) Simple counter
ASP contains five built-in Active Server Components, namely Database Access component, File Access component, Ad Rotator component, Brower Capabilities component, and Content Linking component. The counter we want to design below is to read and write server files through the File Access component (file access component). The algorithm idea is: use a text (ASCII) file on the server to store counted values. Whenever the page is accessed, the value is read out from the file, displayed to the user, and the value is added by 1, and the added value is written back to the file.
The ASP statement and description for writing data to a server count file are as follows:
CounFile=Server.MapPath (the file name used to store the counter value)
Server server access method MapPath (path) converts the path where the file that stores the counter value is located into a physical path.
SET FileObject=Server.CreateObject(Scripting.FileSystemObject)
Use method CreateObject to define object FileSystemObject
SET OutStream=Server.CreateTextFile(FileObject,True,False)
Use the object FileSystemObject to provide the method CreateTextFile to generate a text file, where the parameter True means overwriting the original file, and False means that the file is of type ASCII
OutStream.WriteLine Data to be written
OutStream.WriteLine writes a line of data to the file
The ASP syntax for reading data from a server file is as follows:
CounFile=Server.MapPath (the file name used to store the counter value)
SET FileObject=Server.CreateObject(Scripting.FileSystemObject)
SET InStream=Server.OpenTextFile(FileObject,1,false,false)
Use the object FileSystemObject to provide method OpenTextFile to generate text? Where the parameter True means overwriting the original file, False means that the file is ASCII type to read data = InStream.ReadLine, where InStream.ReadLine is a line of data read from the file.
The following is an example of a counter that uses ASP to implement the page counter function (simplecounter.asp). I have commented 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 ASP, and you have created a text file simplecounter.txt with content 0 in the directory where the home page is located.
Simple ASP counter simplecounter.asp code and comments:
| The following is the quoted content: $#@60;% CountFile=Server.MapPath(simplecounter.txt) The file aspconter.txt is a text file used to store numbers. The initial content is generally 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
| The following is the quoted content: SET FileObject=Server.CreateObject(Scripting.FileSystemObject) Set Out=FileObject.CreateTextFile(CountFile,TRUE,FALSE) Application.lock |
Method Application.lock prohibits other users from changing the counter value
counter= counter 1
The value of the counter is increased by 1
Out.WriteLine(counter)
Write new counter value to the file
Application.unlock
After using the method Application.unlock, allow other users to change the counter value
| The following is the quoted content: Response.Write (You are the first one) Response.Write($#@60;font color=red$#@62;) Response.Write(counter) |
Transfer the counter value to the browser and display it to the user in red color
| The following is the quoted content: Response.Write($#@60;/font$#@62;) Response.Write(a visitor) Out.Close |
Close the file
%$#@62;
(II) Counter separated from the page
In actual applications, the main page and the counter program are separated. Page counting can be achieved by adding a quoted code to the page that needs to be counted. This is the free counters we often apply for online use, but they are usually made by CGI. Here, we just need to slightly modify the simple counter we made with ASP before, and then add a JavaScript statement to the page to reference it, which will realize the counter function separate from the page. In this way, it is very convenient whether it is a counter for the main page or a count for a specific page. Obviously, you need to simply change the file name and counter asp source code file name that store counter values to implement multiple counters.
Counter txtcounter.asp code separated from the page:
| The following is the quoted content: $#@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.Writedocument.write(&counter&) |
In order to correctly display the counter value on the page, call the VBScript function Document.write
Out.Close
%$#@62;
Add the following code to the page 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;
//When referring to the server and directory path where the ASP counter is located.
$#@60;/script$#@62;
$#@60;/font$#@62;
A visitor
$#@60;/p$#@62;
(III) Image counter separate from the page
People’s pursuit is endless, maybe you need a more personalized graphic number counter instead of a simple text number counter. No problem, now let’s take a look at how to implement the graphics counter function using ASP. To implement a graph counter, the key point is how to convert the data value in the counter file into the corresponding graphical representation. Because the decimal number has ten different numbers, including 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, we need to have ten corresponding images, and the file name of the image must correspond to the displayed number. For example, the file name of the image corresponding to 0 is 0.gif, and 1 is 1.gif,... (The image can be made by using tools such as Photoshop, or downloaded from the Internet). Here we want to use the VBScript functions Len(string | varname) and Mid(string,start[,length]). The number of bits of the counter value can be obtained from Len(counter), and the number on the i-th bit of the counter value can be obtained from Mid(counter,i,1). We can use this value to call the corresponding digital image. Using the For loop statement, it is not difficult to obtain the numbers on each bit of the counter value and convert them into the corresponding digital image, so that we can realize the transformation from text value to image number. Below is an example of an image counter written in ASP. Since most of the code has been analyzed before, only some statements are annotated in the code.
Image counter imgcounter.asp code isolated from page:
$#@60;% @language=VBScript%$#@62;
$#@60;%
dim images(20)
Define an array to store statements that display each digit image
| The following is the quoted content: 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 the number of bits of the counter value
| The following is the quoted content: for i=1 to county 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 image corresponding to the numerical value in each bit is obtained from the loop statement and stored in the array. Please pay attention to the actual server and directory path of the image when using it specifically.
response.writedocument.write(&images(i)&);
Call the function Document.write to output the html code that displays digital images
next
Out.Close
%$#@62;
Add the following code to the page to count:
$#@60;p$#@62;
You are the first
$#@60;script language=JavaScript src=http://202.101.209.75/asptemp/counter/imgcounter.asp$#@62;
//When referring to the server and directory path where the ASP counter is located.
$#@60;/script$#@62;
A visitor
$#@60;/p$#@62;
Note: All the above asp counters were tested and passed under Windows NT Server 4.0 (Chinese) / IIS3.0. Any of the following environments can execute ASP:
1. Windows NT Server 4.0 / IIS3.0 or above
2. Windows NT WorkStation 4.0 / Microsoft Peer Web Service 3.0 or above
3. Windows 95/98 / Microsoft Personal Web Server 1.0a or above
Share: ASP example explanation: Use page breaks to realize the display of long article paging Long article pages are displayed with page breaks. If you want to use ASP to implement this function, you can find that only the method of paging is based on the number of words. However, this method has a bug, that is, if there is UBB or HTML code in your article content, it can easily cause it to go between [code] [/code]