利用 Scripting.FileSystemObject 讀寫文字檔記錄點擊數,每次頁面載入遞增並顯示。適合低流量靜態頁面,高流量下檔案鎖定會成為瓶頸。
<%
Set fs = CreateObject("Scripting.FileSystemObject")
Wfile = Server.MapPath("") & "\cgi-bin\www.111cn.cn\asp\asp.html.txt"
On Error Resume Next
Set a = fs.OpenTextFile(Wfile)
hits = CLng(a.ReadLine)
hits = hits + 1
a.Close
If Err.Number <> 0 Then
hits = 1
Err.Clear
End If
Set a = fs.CreateTextFile(Wfile, True)
a.WriteLine(hits)
a.Close
%>
點擊次數:<% = hits %>
Application 物件暫存計數,定期批次寫入檔案,減少 I/O。INCR 指令。Application.Lock 保護檔案讀寫區段(單機環境)。