ASP SCRIPT: 计数器(使用GrapShot组件)

2008-02-05 10:35:42  作者

  这个程序将记数器的数字放在ACCESS数据库中,当然你也能用你希望其它的ODBC数据源.这个程序从URL中读取记数信息.如下:

%26lt; IMG src="id=MYTEST%26amp;seq=default%26amp;cells=7" %26gt;

参数
id

  客户网页的标致(即用户名).每次你用一个新的ID,那么就会有一个新的记录插入数据库中.假如这个ID是原来就存在,那么它的count就增加1.假如你不想让客户自动启动一个ID,而需要注册,那么你就将"自动插入"的值,赋为False.

seq

  可选项.用于选择计数器图片的式样,在客户端所显示的图片的名字就为.gif.缺省值"defalut.gif",而且这个"defalut.gif"必须放在当前工作的目录中!

cells

  可选项.计数结果显示的位数,缺省是5位(这个值你在程序中,也可以调节) 怎样运行这个程序
把这段程序拷贝到服务器中你想要的地方.

  创建一个数据源"Webcounter".这个数据源中必须有个表名字为"main",以下是这个表中的字段:

%26lt; table border="1" cellpadding="3" cellspacing="1" %26gt;
%26lt; tr %26gt;
%26lt; th %26gt;Field%26lt; /th %26gt;
%26lt; th %26gt;Type%26lt; /th %26gt;
%26lt; th %26gt;Indexed%26lt; /th %26gt;
%26lt; th %26gt;Means%26lt; /th %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td %26gt;ident%26lt; /td %26gt;
%26lt; td %26gt;AutoNumber%26lt; /td %26gt;
%26lt; td %26gt;Yes%26lt; /td %26gt;
%26lt; td %26gt;For your needs%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td %26gt;id%26lt; /td %26gt;
%26lt; td %26gt;Text%26lt; /td %26gt;
%26lt; td %26gt;Yes%26lt; /td %26gt;
%26lt; td %26gt;用户名%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td %26gt;count%26lt; /td %26gt;
%26lt; td %26gt;Number%26lt; /td %26gt;
%26lt; td %26gt;No%26lt; /td %26gt;
%26lt; td %26gt;计数数字%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; /table %26gt;


需要时给数据库赋一个的权限

  建立一个存放GIF图形的目录,在这个目录中至少要放一个default.gif的图形组.适当时给这个文件以权限.

给这个目录中放一些能被10整除的数字图形.

%26lt; table width="100%" border="1" cellspacing="1" cellpadding="2" %26gt;
%26lt; tr %26gt;
%26lt; td bgcolor="#ccffff" %26gt;File %26lt; b %26gt;counter.asp%26lt; /b %26gt; %26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td bgcolor="#ffffcc" %26gt;%26lt; pre %26gt;
%26lt; %
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='" %26amp; id %26amp; "'"
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=" %26amp; ident)
conn.close()
ShowNumbers()
else
rs.close()
if auto_insert=True and id%26lt; %26gt;"" then
conn.Execute("insert into main (id,count) values ('" %26amp; id %26amp; "',1)")
count=1
ShowNumbers()
end if
conn.close()
end if

Sub ShowNumbers()
set g=CreateObject("shotgraph.image")
filename=workdir %26amp; seq %26amp; ".gif"
if g.GetFileDimensions(filename,xsize,ysize,pal)%26lt; %26gt;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

% %26gt;
%26lt; xmp %26gt;
%26lt; /pre %26gt;
%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; /table %26gt;

%26lt; hr %26gt;%26lt; /span %26gt;%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td width="50%" %26gt; %26lt; /td %26gt;
%26lt; td width="50%" %26gt;%26lt; br %26gt;%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td colspan=2 align="right" %26gt;
%26lt; img border="0" src="images/bbstitle.gif" %26gt;%26lt; a id="hlkAddMsg" href="/AddMsg.aspx?id=234" style="color:red" %26gt;发表评论%26lt; /a %26gt;
%26lt; img border="0" src="images/mail.gif" %26gt;%26lt; a id="hlkMailTo" href="/MailTo.aspx?id=234" %26gt;邮寄本页%26lt; /a %26gt;
%26lt; img border="0" src="images/print.gif" %26gt;%26lt; a href="javascript:window.print()" %26gt;打印本页%26lt; /a %26gt;
%26lt; img border="0" src="images/profile.gif" %26gt;%26lt; a href="javascript:void(null)" onclick="if(!document.execCommand('SaveAs','show.aspx',1)) return false;" %26gt;保存本页%26lt; /a %26gt;
%26lt; img border="0" src="images/ask.gif" %26gt;%26lt; a href="bbs/index.asp" %26gt;您有疑问%26lt; /a %26gt;
%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr bgcolor="#97B9FF" %26gt;
%26lt; td width="50%" %26gt;%26lt; b %26gt;%26lt; font color="#FFFFFF" %26gt;相关文章%26lt; /font %26gt;%26lt; /b %26gt;%26lt; /td %26gt;
%26lt; td width="50%" %26gt;%26lt; b %26gt;%26lt; font color="#FFFFFF" %26gt;相关评论%26lt; /font %26gt;%26lt; /b %26gt;%26lt; /td %26gt;
%26lt; /tr %26gt;
%26lt; tr %26gt;
%26lt; td width="50%" valign="top" %26gt;


%26lt; /td %26gt;
%26lt; td width="50%" valign="top" %26gt;

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