[Tutor] Hit Counter

Jon Cosby jcosby@wolfenet.com
Sun, 29 Jul 2001 07:19:06 -0700


I'd like to write a hit counter for my Web page. As easy as this
would seem, it's got me stumped. I'm using a hidden form field:

<form action="cgi-bin/counter.pyw">
<input type="hidden" name="Stats" value="Hit Counter">
</form>

but I suspect this is where the problem is. Why doesn't the script
run each time the page loads?

Jon Cosby

-----------------Begin Script--------------------------------------

#! c:\python21\pythonw.exe

print "Content-type: text/html"
print
print "<HEAD><TITLE>Python Hit Counter</TITLE></HEAD>"
print "<BODY>"


form = cgi.FieldStorage()
if form.has_key("Stats")			# Hidden field
	InFile = open("count.dat", "r")	# Text file with total hits
	Hits = InFile.readline()
	x = int(Hits) + 1
	h = str(x)
OutFile = open("count.dat", "w")
OutFile.write(str(x))
OutFile.close()


print "<P><H2>Jon Cosby's web page - Total hits:</H2><br><br> "
for i in h:
	print "<img src='images/" + i + "silver.gif'>"
print "</BODY>"

--------------End Script---------------------------------------------