There are usually two simple methods to create a webpage counter using ASP. One is to create global. Asa, and the other is to directly write an ASP file for counting. Generally, a document is used to save the browsing quantity.
1. Use global. Asa to write a counter and write a global. Asa file, which is stored in the root directory of the virtual directory. The source code is as follows:
<Script language = "VBScript" runat = "server">
Sub application_onstart ()
Countfile = server. mappath ("counter") + "/counter.txt"
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set file = FSO. opentextfile ("countfile ")
/'Read the number of shards and assign it to the application variable number
Application ("Number") = file. Readline
File. Close
End sub
Sub session_onstart ()
If isempty (Session ("hasbeenconnected") then
Application. Lock
Application ("Number") = Application ("Number") + 1
Applicaiotn. Unlock
End if
Session ("hasbeenconnected") = true
End sub
Sub application_onend ()
Countfile = server. mappath ("counter") + "/counter.txt"
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set file = FSO. createtextfile ("countfile", true)
/'Use the writeline method to write the current value
File. writeline (Application ("Number "))
File. Close
End sub
</SCRIPT>
When calling the counter, write <% response. Write ("you are the visitor" & number! ") %> Yes, but the called webpage must also be an ASP page. The disadvantage of this method is that many personal homepage spaces do not support running global. Asa, that is, they are not virtual directories created for users, so they cannot run properly.
2. Write a counter. asp directly to count the counters I currently use. It can be called on any page:
<Scriptsrc = "http://xxx.xxx.xxx/counter.asp? Id = ABC & num = 6 & Style = 1 "> </SCRIPT>
Where id = ABC indicates that the user name is ABC, so you need to create counter/abc.txt to store the Count value;
Num = 6 indicates the number of digits displayed on the counter;
Style = 1 is the counter style, and counter/style1/0 ~ is created ~ 9.gif. You can add multiple styles.
The source code is as follows:
<%
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Filepath = server. mappath ("counter") + "/" + Request ("ID") + ". txt"
Set temp = FSO. opentextfile (filepath, 1)
Count = temp. Readline
Temp. Close
If isempty (Session ("connected") then
Set temp = FSO. opentextfile (filepath, 2)
Application. Lock
Count = count + 1
Temp. writeline (count)
Application. Unlock
Temp. Close
End if
Set temp = nothing
Set FSO = nothing
Session ("connected") = true
Numlength = Len (count)
If request ("num") = "" then
Maxnum = 6
Else
Maxnum = CINT (Request ("num "))
End if
If request ("style") = "" then
Style = "1"
Else
Style = request ("style ")
End if
For I = 1 to maxnum Step 1
If I <= maxnum-numlength then
Countimage = " </img>"
Response. Write "document. Write (/'" & countimage &"/');"
Else
Countimage = " </img>"
Response. Write "document. Write (/'" & countimage &"/');"
End if
Next
%>