萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> ASP 程式設計

ASP 簡易文字計數器:以檔案儲存點擊數

原理

利用 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。
  • 高流量網站改用資料庫計數器或 Redis INCR 指令。
  • 檔案路徑勿放在網站公開目錄,防止被直接下載。
  • 考慮使用 Application.Lock 保護檔案讀寫區段(單機環境)。
copyright © 萬盛學電腦網 all rights reserved