隨著網(wǎng)絡(luò)大行其道,網(wǎng)頁計(jì)數(shù)器也流行起來。事實(shí)上大多數(shù)網(wǎng)站均有網(wǎng)頁計(jì)數(shù)器,用以反映該網(wǎng)站的訪問量。計(jì)數(shù)器的來源很廣,F(xiàn)rontpage等網(wǎng)頁編輯器自帶了網(wǎng)頁計(jì)數(shù)器,有的站點(diǎn)也提供免費(fèi)的計(jì)數(shù)器下載。其實(shí)熟悉了asp編程后,自己做一個(gè)計(jì)數(shù)器很容易。下面介紹一種實(shí)現(xiàn)方法。 計(jì)數(shù)器原理是:在第一次使用網(wǎng)頁時(shí)置初始值1,以后每請求網(wǎng)頁一次,將計(jì)數(shù)器值加1。這樣我們只要在服務(wù)器上放置一個(gè)文本文件counter.txt,文本文件的內(nèi)容有計(jì)數(shù)器的值,以后每請求一次頁面,讀出文本文件的計(jì)數(shù)器的數(shù)值,加1顯示,然后再將原來的值改變?yōu)榧?后的值,保存到文本文件。至于初始置1,在服務(wù)器上可先不建counter.txt,在網(wǎng)頁中,先判斷服務(wù)器上是否有counter.txt文件,沒有就生成counter.txt,在counter.txt中寫入1,網(wǎng)頁上顯示計(jì)數(shù)器值1,完成初始置1。以后每次只要到指定目錄下將counter.txt文件刪除即可置初始值。 具體操作時(shí)要有顯示數(shù)字0、1、2….9的圖像文件,0.gif、1.gif、2.gif…9.gif,文件不能太大,一般18*25即可。將你要放計(jì)數(shù)器的網(wǎng)頁布局設(shè)計(jì)完畢,再改成ASP文件,將下面代碼輸入到要顯示計(jì)數(shù)器的地方,使用時(shí),程序?qū)⒆詣?dòng)在虛擬目錄count下建立counter.txt文件。置初始值時(shí)將文件刪除即可。對了,虛擬目錄count必須給everyone有寫的權(quán)限。 <% Const ForReading = 1, ForWriting = 2, ForAppending =3 Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0 filepath=server.mappath("/count") filename=filepath+"/counter.txt" set fs=createobject("scripting.filesystemobject") if fs.fileexists(filename) then
set f=fs.getfile(filename) Set ts = f.OpenAsTextStream(ForReading, TristateUseDefault) s=ts.readline+1 ts.close else fs.createtextfile(filename) set f=fs.getfile(filename) s=1 end if
'向counter.txt中寫數(shù)據(jù) Set ts = f.OpenAsTextStream(ForWriting, TristateUseDefault) ts.writeline(cstr(s)) ts.close
'顯示計(jì)數(shù)器 s=cstr(s+1000000) s=mid(s,2,6) for i=1 to 6 response.write "<img src='../images/"&mid(s,i,1) &".gif' width='18' height='25'>" next
更精確一點(diǎn),只要將上面的代碼略加修改,放到你的global.asa的session_onstart中,這樣,只有新用戶進(jìn)入網(wǎng)站,計(jì)數(shù)器才會(huì)加1。已經(jīng)進(jìn)入網(wǎng)站的用戶刷新頁面,不會(huì)引起計(jì)數(shù)器計(jì)數(shù)的改變,而且不管你從哪個(gè)頁面進(jìn)站,計(jì)數(shù)器都能捕捉到你。 <script language=vbscript runat=server> sub application_onstart filepath=server.mappath("/count") filename=filepath+"/counter.txt" set fs=createobject("scripting.filesystemobject") if not fs.fileexists(filename) then fs.createtextfile(filename) set f=fs.getfile(filename) s=1 Set ts = f.OpenAsTextStream(2, -2) ts.writeline(cstr(s)) ts.close else set f=fs.getfile(filename) Set ts = f.OpenAsTextStream(1, -2) s=ts.readline+1 ts.close end if application(“visitor”)=s end sub
sub session_onstart session.timeout=5 application(“visitor”)=application(“visitor”)+1 set f=fs.getfile(filename) Set ts = f.OpenAsTextStream(2, -2)
ts.writeline (cstr(application(“visitor”))) ts.close end sub </script> 在網(wǎng)頁相應(yīng)部分根據(jù)application(“visitor”)的值顯示計(jì)數(shù)器的圖像。 <% s=cstr(application("visitor")+10^6) s=mid(s,2,6) for i=1 to 6 response.write "<img src='../images/" &mid(s,i,1)&".gif' width='18' height='25'>" next %> 要顯示n位計(jì)數(shù)器只要將上面代碼改為: <% s=cstr(application(“visitor”)+10 ^n) s=mid(s,2,n) for I=1 to n response.write "<img src='../images/" &mid(s,i,1)&".gif' width='18' height='25'>" next %> 這樣要得到8位計(jì)數(shù)器,只要加上n=8即可。 要是覺得每次有用戶進(jìn)入網(wǎng)站,均對counter.txt寫入太頻繁,可以在session_onstart中設(shè)置,在application (“visitor”)為十的倍數(shù)時(shí)將計(jì)數(shù)值寫進(jìn)counter.txt。 怎么樣?現(xiàn)在就動(dòng)手吧!