Just recently I
moved nedprod.com from its old provider to its new one - unfortunately,
the new one did not support shell access and thus you could not compile
your own CGI. This was a problem as I had been using Muquit's
excellent C-based web counter which meant, unfortunately I was going
to have to change. In looking around to find some other solution which let
me import my old counts, I came across a number of database-driven web
counters which got me thinking. Unfortunately, all the MySQL (this is what
my new host provides) driven web counters were very badly written

After a few hours digging around the docs of this thing called PHP
(which AFAICS is C with webby bits on), I came up with what is below.
Indeed, the very web counter you see at the top of every page on
nedprod.com runs off the same!

Features:

Implemented using PHP4 for MySQL databases

Automatically extracts what web page it is embedded within so you
don't have to (ie; you can insert the same PHP code into every HTML
file)

Automatically creates a new entry if your page is new (ie; you just
make your page & upload)

Is fine with heavily loaded servers (does not lose counts like some
other database-driven web counters)

Is in the public domain (ie; free for use - you don't even need to
say I wrote it as I have given up my copyright)

Since it's so small, I'm not even going to offer it as a download - you
can simply copy & paste it from here. Note that this is a live copy - I
have some PHP mangle the actual version in usage onto this page:

Please note that this is my very first ever PHP program and so
may contain errors!

Installation:

Simply place the above into a "counter.php" file and place
within a directory called "private" within your public_html
directory. Now copy the following text into a file called ".htaccess"
inside your private directory:

<Limit GET POST>
order deny,allow
deny from all
</Limit>
<Limit PUT DELETE>
order deny,allow
deny from all
</Limit>

This ensures that no one can open the counter.php file directly and so
read your password. If your server is not running Apache then this won't
work - if you're on IIS, well quite frankly change to Apache (it runs on
Windows too!).

Next, you must customise your counter.php file to reflect your system's
configuration. All the relevant parts are at the top of the file - you'll
need to specify your base URL so that queries to the database are from the
root of your particular site (ie; if your site is on http://www.mysite.net/~foo
then this will be /~foo). Next you will need to specify which MySQL
database to use plus its username and its password. Chances are you won't
need to alter $dbhost nor $dbtable if the database is running on the same
server hosting your website.

You must now create the counters table in the database you just
entered. Open that database and run the following SQL query:

Last thing to do is how to insert the web counter into your pages. Open
your page in HTML form and at where you want it, insert:

<?php include("<path to home account>/public_html/private/counter.php"); ?>

Usually the path to your home account on the server is
"/home/<username>". You should now find that when opening
the page, the text "<n> times since the <date>th
<month> <year>" will appear where you placed the PHP
insert. The value will increment and the date is set to the first time a
query is made ie; if the page is not in the table, it gets added with
today's date.

You can of course manually edit the database and replace the figures
with any you like. It's best to play with all this using one test page
first before deploying across your website.

Note: This code is provided in the hope that it will be
useful, but without any warranty; without even the implied warranty
of merchantibility or fitness for a particular purpose. If
you lose any data as a result of this code, it's not my fault though I
welcome bug reports.

Getting Apache to execute PHP inserts inside HTML pages:

Something which I scratched my head over, so I'm saving you the bother
- insert the following into a ".htaccess" file at the ROOT of
your web site: