[These pages contain ready ASP scripts. You can use them in your own applications]

ASP SCRIPT: Web counter


Note: all scripts examples included in these Web pages are absolutely free. You can copy, use, and modify these scripts anyway you want. The scripts are published without warranty of any kind.

This script keeps the counter value in the access database. Of course, you can use any other ODBC data source you want. You can use The ASP script reads the images of digits from the digit bars (GIF images containing all digits from 0 to 9).

<IMG SRC="counter.asp?id=MYTEST&seq=default&cells=7">

Script's parameters

id
The identifier of the Web page. Each time you use Web counter with the new unique id, the new record will be added to the database. If such record already exists the Web counter will increase the appropriate value on 1. If you do not want auto inserting new records, so only registered pages could use your counter, you should set auto_insert valuable in the script to False
seq
Optional. The name of the digit sequence. The name of GIF bar file for each sequence is <seq>.gif. The default name is 'default'. The file 'default.gif' should be present in the script working directory.
cells
Optional. The number of digits in the resulting image. The default value is 5.

How to run this script

  1. Copy this script to any chosen by you location on you Web server
  2. Create the system data source named "Webcounter". The corresponding access file has to contain the table named "main". The fields of table are shown below:
    FieldTypeIndexedMeans
    identAutoNumberYesFor your needs
    idTextYesId parameter from the HTTP query string identifies the corresponding Web page
    countNumberNoThe number of hits to the corresponding Web page
    Set appropriate permissions to database file and directory if required.
  3. Create the directory for GIF digit bars placement and copy at least one default.gif digit bar to there. Set file permissions if required. The default.gif bar is here:

    Take into account that the horizontal number of pixels should be divided by 10 without remainder.
    You can find other GIF bars on the great amount Web pages, like DigitMania. Or, of course, you can draw them yourself.

File counter.asp
<%
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

%>


Back to examples