下面代碼是:重繪一次隨機從5到9之間增加訪問次數。資料庫是記事本格式的 count.txt
csdn論壇上有很多網友發帖說: “要是 覺得每次有用戶進入網站,均對counter.txt寫入太頻繁,可以在session_onstart中設定,在application(“visitor”)為 十的倍數時 將計數值寫進counter.txt。
這代碼該怎么修改補充? 請教高手!!!謝謝
<%
'Option Explicit
'變數定義
Dim fileName, fs, txt, Content, counterLenth, displayMode
'屬性設定
counterLenth=0 '設定顯示資料的最小長度,如果小于實際長度則以實際長度為準
displayMode =2 '設定顯示模式,1為文字,2為圖片
fileName=Server.MapPath("count.txt")
Set fs=Server.CreateObject("Scripting.FileSystemObject")
'初始化
Function Init()
'檢查檔案是否存在,不存在則創建,并寫入0
If Not fs.FileExists(fileName) Then
fs.CreateTextFile fileName,True,True
Set txt=fs.OpenTextFile(fileName,2,True)
txt.Write 0
txt.close()
Set txt=Nothing
End If
'快取為空,則讀取文本資料
Set txt=fs.OpenTextFile(fileName,1,1)
If txt.AtEndOfStream Then
Application("Counter")=0 '如果檔案中沒有資料,則初始化Application("Counter")的值(為了容錯)
Else
Application("Counter")=txt.ReadLine
End If
txt.close()
Set txt=Nothing
Application.Lock '利用Application特性防止并發寫入
Randomize
Application("Counter") = Application("Counter") + Int(((9-5+1) * Rnd) + 5)
SaveNum(Application("Counter")) '寫入新資料
Application.UnLock
End Function
'顯示計數函式
Function ReadNum()
Init()
ReadNum=PrintNumber(Application("Counter")) '呼叫顯示方式函式
End Function
'保存資料函式
Function SaveNum(Content)
Set txt=fs.OpenTextFile(fileName,2,True)
txt.Write Content
txt.Close
Set txt = Nothing
End Function
'顯示方式函式
Function PrintNumber(x)
Dim i,MyStr,sCounter
sCounter = Clng(x)
For i = 1 To counterLenth - Len(sCounter)
If displayMode=1 Then
MyStr = MyStr & "0"
Else
MyStr = MyStr & "<IMG SRC=https://img.uj5u.com/2020/11/18/155338180624381.gif>" '如有圖片,可呼叫此陳述句
End If
Next
For i = 1 To Len(sCounter)
If displayMode=1 Then
MyStr = MyStr & Mid(sCounter, i, 1)
Else
MyStr = MyStr & "<IMG SRC=https://bbs.csdn.net/topics/改成你自己的圖片存放的相對目錄/" & Mid(sCounter, i, 1) & ".gif>" '如有圖片,可呼叫此陳述句
End If
Next
PrintNumber = MyStr
End Function
%>
uj5u.com熱心網友回復:
asp網站?你這個不是“資料庫是txt后綴”,只是你用了一個txt做狀態存盤而已
如果 是asp的話,我記得Application是全域性的呀。好像是有個Global.xxx什么后綴的檔案中就有你說的那些Application_OnStart或者Application_OnEnd?之類的事件。你按這個思路去網上搜搜吧
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/223489.html
標籤:JavaScript
