by Niall Douglas. Last updated . This page has been accessed 15,577 times since the 5th December 2003.
|View this page in:||English||Any language:
Translation to non-English languages provided by Google Language
You are connecting to the IPv4 version of this website from the IP address 188.8.131.52. You can try the IPv6-only version if you want.
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!
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!
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:
CREATE TABLE counters ( id int(10) unsigned NOT NULL auto_increment, page varchar(255) , count bigint(20) unsigned DEFAULT '0' NOT NULL , created date DEFAULT '0000-00-00' NOT NULL , PRIMARY KEY (id) , INDEX (page) );
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:
AddType application/x-httpd-php .html .php .htmAnd that's all there is to it!