On the Internet, counters written in ASP are everywhere. I feel that it is meaningless to talk about counters written in ASP here. Later, I thought it was necessary to talk about it, because this is not just about writing counters, but also introducing the knowledge and application skills of ASP to everyone.
The original code of the program is as follows:
$#@60 ;%countlong=8
'countlong defines the number of display digits of the counterOn Error Resume Next
′ Ignore all errors count=server.mappath(″count.txt″)
set cou = server.createobject(″scripting.filesystemobject″)
set out=cou.opentextfile(count,1,false,false)
counter=out.readline
out.close
′The above code is to open the file count.txt and read a line to read the current counter value if session(″in″)$#@60;$#@62;″in″ then
'Determine whether the user has entered the homepage to ensure that the counter will not increase by 1 when he presses refresh or comes to this page again.
session(″in″)=″in″
set out = cou.createtextfile(count,true,false)
application.lock
counter=counter+1
'Counter plus 1
out.writeline(counter)
application.unlock
out.close
'The above code is to create a new file count.txt and write a line, write the value of the counter after adding 1 end if
co=cstr(counter)
The 'cstr function is used to convert numeric variables into character types for i = 1 to countlong-len(co)
response.write ″″
next
'The above loop statement is to complete the addition of the 0 pictures in front of the counter'The len(co) function is to get the length of the variable co, countlong-len(co) gets the number of the previous 0 pictures for i= 1 to len(co)
response.write ″″
next
The ′mid(co,i,1) function is used to read a character % starting from the i-th position of variable co $#@62 ;
The most successful part of this counter is to use session to prevent users from refreshing the counter multiple times; a text file is used to record the value of the counter. Although the code is longer to write, it is much better than using the application object, because when the server reboots or When global.asa is changed, the value of the application object will be cleared; and pictures are used instead of characters, which looks more beautiful!
You can design the picture of the counter yourself. Use the file name 0.gif for 0, 1.gif for 1, and so on.