asp中如何實現網頁計數器

     在ASP中,如何實現網頁的計數器呢?其實奧妙全在於站點根目錄下的GLOBAL.ASA文件下

    大家知道,在ASP腳本語言中,有兩個對象比較重要,一個是應用程序對象APPLICATION,一個是 會話對象SESSION,前者是全局的,後者在用戶訪問網絡的時間內有效,是基於訪問者的.

     當第一位訪問者瀏覽應用程序時,系統便會執行APPLICATION_ONSTART和SESSION_ONSTART,當第二位訪問者以及後面的訪問者瀏覽時,便會執行SESSION_ONSTART,當訪問者離開或註銷時,就執行SESSION_ONEND,當你的應用程序關閉或重啓時,APPLICATION_ONEND便執行了,知道了函數編譯的順序和原理,我們就可以給自己的個人主頁添磚加瓦了

     首先,在你網頁根目錄下,建立文件GLOBAL.ASA

     文件內容如下:

<%@ Language="VBScipt" runat=server%>

%

Sub Application_OnStart

Application("visit") = GetVisitNum()//得到文本文件中的訪問總數

Application("active")=0                     //將在線人數初始化位0

End Sub

Sub Session_OnStart

Application.lock()

Application("visit") =Application("visit") +1

Application("active")=Application("active")+1

Application.unlock()

End Sub

Sub Session_OnEnd

Application.lock()

Application("active")=Application("active")-1

Application.unlock()

End Sub

Sub Application_OnEnd

SaveVisitNum()

End Sub

function GetVisitNum()

Set fso = Server.CreateObject("Scripting.FileSystemObject")
Application("strCounterFileName") = Server.MapPath("/guestbook/counter.txt")
Set fsoCounterFile = fso.OpenTextFile(Application("strCounterFileName"),1,true)
if(fsoCounterFile.AtEndOfStream) then
   GetVisitsNum = 0
else
   GetVisitsNum = fsoCounterFile.ReadLine()
End if
fsoCounterFile.Close()

dim strFileName=Server.MapPath("")

End function

Function SaveVisitsNum
Set fso = Server.CreateObject("Scripting.FileSystemObject")
Set fsoCounterFile = fso.CreateTextFile(Application("strCounterFileName"),true)
fsoCounterFile.WriteLine(Application("visits"))
fsoCounterFile.Close()
End Function

%>

 

然後建立一個測試網頁TEST.ASP

如下:

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>無標題文檔</title>
</head>

<body>
<p>訪問人數<%= Application("visits") %></p>
<p>在線人數<%= Application("active") %></p>
</body>
</html>

是不是可以計數了

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章