A web page counter

Try online Examples

Counters are very popular CGI applications, and are also a useful tool to easily keep an eye on how well a page is doing.
The counter above has been implemented in Aptilis. Several things make Aptilis a great tool to develop counters.
They are:

Note that you don't have to actually display the hit count, you can use any graphic or a white pixel. This allows you to keep an idea of the number of hits for a particular page without telling the public.

Hit refresh to see the number of hits increasing. Nothing might happen if you are accessing the net through a proxy that will insist on caching the graphic. Unfortunately, that's not something Aptilis - or any other language, can address.

Here is how the picture above is called:

	<IMG src="/cgi-bin/aptilis.exe?file=/home/scripts/counter.e.txt">
That's how I did it on this machine. Of course /home/scripts/counter.e.txt is the script below, and you should replace that and /cgi-bin/aptilis.exe by appropriate values, ie. http://www.yourserver.com/cgi-bin/aptilis.exe.

Here's the source:

sub main

	fontPath = "c:/winnt/fonts/times.ttf" $
	path = "c:/temp/aptilis/eq-counter.dat" $

	// Let's try to get a lock on the hits file.
	maxWait = 10
	t = getTime()
	while lock(path$) = -1
		if getTime() - t > maxWait
			failed = 1
			break
		end if
	end while

	if failed = 1
		hits = "oops" $
	else
		// load the hits, and increment them
		hits = loadFile(path$)
		saveFile(path$, int(hits +1)$)

		// give it back
		unlock(path$)
	end if

	// Now we can take our time with the graphic
	b = CreateBitmap(114, 20)
	if b != -1

		print("Content-type: image/gif\n\n")

		// White bacground
		clearBitmap(b, 255)

		// anything is possible, but OK, I won't get any design awards on that one!
		if p = 0 or p = 5

			if p = 0
				box(b, 0, 0, 15, 10, RGB(255, 0, 0), 1)
				box(b, 0, 0, 15, 10, 0, 0)
			end if

			// now that's the point: let's use a nice font!
			setFont(b, fontPath$, 14)

			mess = int(hits) + "hits" $
			getStringMetrics(b, mf[], mess$)
			printAt(b, (114 - mf[0]) / 2, 15, int(hits) + " hits"$, 0)
		end if
		outputGIFBitmap(b)

		// because we're tidy
		deleteBitmap(b)

	else
		print("Content-type: text/plain\n\n")
		print("It was impossible to create the bitmap.\n")
	end if

end sub

Things learnt