單頁面訪問計數,資料定期寫入文字檔,重啟不遺失。
Set MyPageCounter = Server.CreateObject("MSWC.PageCounter")
MyPageCounter.PageHit ' 增加計數
n = MyPageCounter.Hits ' 取得計數,可指定頁面路徑
MyPageCounter.Reset ' 歸零,可指定頁面路徑
全站單一實例管理多個具名計數器,資料儲存於 Counters.txt。建議在 global.asa 宣告應用程式層級實例:
<OBJECT RUNAT="Server" SCOPE="Application" ID="Counter" PROGID="MSWC.Counters"></OBJECT>
| 方法 | 說明 |
|---|---|
| Get(name) | 取得計數值,不存在則建立並歸零 |
| Increment(name) | 遞增並回傳新值,不存在則建立並設為 1 |
| Set(name, value) | 設定指定整數值 |
| Remove(name) | 移除計數器 |
<%
Set MyPageCounter = Server.CreateObject("MSWC.PageCounter")
MyPageCounter.PageHit
If Request("reset") = "on" Then MyPageCounter.Reset()
%>
<font face="華文彩雲" size="50" color="#cc9999">
這個網頁被瀏覽了 <%=MyPageCounter.Hits%> 次。
</font>
<form method="get" action="PageCounter.asp">
<input type="submit" value="點擊網頁">
<input type="checkbox" name="reset">重置網頁計數器
</form>
兩種組件適用不同場景:PageCounter 適合單頁獨立統計,Counters 適合全站多計數器集中管理。