ASP SCRIPT: 計數器(使用GrapShot組件)

ASP SCRIPT: 計數器

  這個程序將記數器的數字放在ACCESS數據庫中,當然你也能用你希望其它的ODBC數據源.這個程序從URL中讀取記數信息.如下:

< IMG src="id=MYTEST&seq=default&cells=7" >

參數
id

  客戶網頁的標緻(即用戶名).每次你用一個新的ID,那麼就會有一個新的記錄插入數據庫中.如果這個ID是原來就存在,那麼它的count就增加1.如果你不想讓客戶自動啓動一個ID,而需要註冊,那麼你就將"自動插入"的值,賦爲False.

seq

  可選項.用於選擇計數器圖片的式樣,在客戶端所顯示的圖片的名字就爲.gif.缺省值"defalut.gif",而且這個"defalut.gif"必須放在當前工作的目錄中!

cells

  可選項.計數結果顯示的位數,缺省是5位(這個值你在程序中,也可以調節) 怎樣運行這個程序
把這段程序拷貝到服務器中你想要的地方.

  創建一個數據源"Webcounter".這個數據源中必須有個表名字爲"main",以下是這個表中的字段:

< table border="1" cellpadding="3" cellspacing="1" >
< tr >
< th >Field< /th >
< th >Type< /th >
< th >Indexed< /th >
< th >Means< /th >
< /tr >
< tr >
< td >ident< /td >
< td >AutoNumber< /td >
< td >Yes< /td >
< td >For your needs< /td >
< /tr >
< tr >
< td >id< /td >
< td >Text< /td >
< td >Yes< /td >
< td >用戶名< /td >
< /tr >
< tr >
< td >count< /td >
< td >Number< /td >
< td >No< /td >
< td >計數數字< /td >
< /tr >
< /table >


需要時給數據庫賦一個的權限

  建立一個存放GIF圖形的目錄,在這個目錄中至少要放一個default.gif的圖形組.適當時給這個文件以權限.給這個目錄中放一些能被10整除的數字圖形.

< table width="100%" border="1" cellspacing="1" cellpadding="2" >
< tr >
< td bgcolor="#ccffff" >File < b >counter.asp< /b > < /td >
< /tr >
< tr >
< td bgcolor="#ffffcc" >< pre >
< %
Response.ContentType="image/gif"
Response.Expires=0
'The directory where GIF bars are placed. Ends with /
workdir="c:/gifbars/"
'Set this to False if you want to disable auto inserting
'new counter records to database on every hit with unique
'id string
auto_insert=True
id=Trim(Request.QueryString("id"))
seq=Trim(Request.QueryString("seq"))
if seq="" then seq="default"
cells=Trim(Request.QueryString("cells"))
if cells="" then cells=5 else cells=cells*1
set conn=Server.CreateObject("ADODB.Connection")
conn.Open "Webcounter"
sql="select count,ident from main where id='" & id & "'"
set rs=conn.Execute(sql)
if not rs.EOF then
count=rs("count")+1
ident=rs("ident")
rs.close()
conn.Execute("update main set count=count+1 where ident=" & ident)
conn.close()
ShowNumbers()
else
rs.close()
if auto_insert=True and id< >"" then
conn.Execute("insert into main (id,count) values ('" & id & "',1)")
count=1
ShowNumbers()
end if
conn.close()
end if

Sub ShowNumbers()
set g=CreateObject("shotgraph.image")
filename=workdir & seq & ".gif"
if g.GetFileDimensions(filename,xsize,ysize,pal)< >1 then Exit Sub
xdigit=xsize/10
g.CreateImage xdigit*cells,ysize,UBound(pal)+1
g.InitClipboard xsize,ysize
g.SelectClipboard True
for i=0 to UBound(pal)
g.SetColor i,pal(i,0),pal(i,1),pal(i,2)
next
g.ReadImage filename,pal,0,0
for i=1 to cells
k=GetDigit(count,cells-i)
g.Copy (i-1)*xdigit,0,xdigit,ysize,k*xdigit,0,"SRCCOPY"
next
Response.BinaryWrite g.GifImage(-1,1,"")
End Sub

Function GetDigit(number,position)
number1=number/(10^position)
tmp=number1/10
GetDigit=number1-tmp*10
End Function

% >
< xmp >
< /pre >
< /td >
< /tr >
< /table >

< hr >< /span >< /td >
< /tr >
< tr >
< td width="50%" > < /td >
< td width="50%" >< br >< /td >
< /tr >
< tr >
< td colspan=2 align="right" >
< img border="0" src="images/bbstitle.gif" >< a id="hlkAddMsg" href="/AddMsg.aspx?id=234" style="color:red" >發表評論< /a >
< img border="0" src="images/mail.gif" >< a id="hlkMailTo" href="/MailTo.aspx?id=234" >郵寄本頁< /a >
< img border="0" src="images/print.gif" >< a href="javascript:window.print()" >打印本頁< /a >
< img border="0" src="images/profile.gif" >< a href="javascript:void(null)" οnclick="if(!document.execCommand('SaveAs','show.aspx',1)) return false;" >保存本頁< /a >
< img border="0" src="images/ask.gif" >< a href="bbs/index.asp" >您有疑問< /a >
< /td >
< /tr >
< tr bgcolor="#97B9FF" >
< td width="50%" >< b >< font color="#FFFFFF" >相關文章< /font >< /b >< /td >
< td width="50%" >< b >< font color="#FFFFFF" >相關評論< /font >< /b >< /td >
< /tr >
< tr >
< td width="50%" valign="top" >


< /td >
< td width="50%" valign="top" >

< /td >
< /tr >
< tr >
< td width="50%" >
< /td >
< td width="50%" align="right" >< img border="0" src="images/more.gif" >< a id="hlkRela" >< /a >< /td >
< /tr >
< /table >
< /td >
< /tr >
< /table >
< /td >
< /tr >
< /table >
< /body >
< /html >


 
發佈了2 篇原創文章 · 獲贊 4 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章