S LogoS Logo

PHP Counter

Lots of people want hit counters on their web sites. Some of those people know how to write their own, but most don't. For the benefit of those that don't know how, I have made the counter used on my site available for download.

For the benefit of anybody who doesn't know what a hit counter is, it is a script that keeps track of how many times a page has been loaded.

For example:

[2479]

Each time you refresh the page, the count will rise by 1.

This counter can be a little tricky to set up, but has the advantage over many CGI counters that it is password protected, so nobody can "latch" onto your counter, unless they know your username and password. Should that happen though, you can just change them.

Requirements

My counter is written using PHP and MySQL, so to use it, you need access to a web server that supports PHP and MySQL. If you are unsure, create a file called test.php, the open it in a text editor such as notepad or vi, and enter the following:


<?php

phpinfo();

?>

Next upload the file to your webserver and then try to view it in your web browser. If you get an error message, or you are asked if you want to download the file, then your webserver doesn't support PHP. If the server does support php, then you should see a some tables of information about the PHP configuration on the server. Look for the table called mysql and see if it is enabled. If MySQL is enabled, then you are ready to go. If either of the above did not work, then try contacting your webspace provider. If that happens to be your ISP, then you probably won't get anywhere, but if it is within your company or you are paying for the service, and you really suck up to the administrator, you may be in luck.

The script

Ok, so I assume that if you are still reading, then everything above worked, and you are ready to go. Download counter.txt. Feel free to rename the file to whatever suits your needs, you don't have to keep the same extension either (mine is called counter.php, but you wouldn't be able to download a link to a .php file, so I had to rename it).

Next open the file in a text editor and look for the following section near the top:


// The server that is hosting the MySQL database
$host = "localhost";

// Your username for the database
$dbusername = "INSERT YOUR USERNAME HERE";

// Your password for the database
$password = "INSERT YOUR PASSWORD HERE";

// The name of the database
$database = "INSERT THE DATABASE NAME HERE";

// The location of the counter images
$imagepath = "/images/counter";

You will need to change these bits to reflect your setup.

$host - This is the name of the computer that is running the MySQL server. If this is the same computer that is hosting your PHP pages, then you can enter "localhost" here. It is unlikely that you will need to change this, however if the MySQL server is on another computer, for example, if your web-hosting company doesn't provide you with a databse, but somebody has given you permission to use theirs, then you should enter the full URL here (e.g. "http://www.somesite.com").

$dbusername - The username that you use to access the database. By default, this will probably be the same as the username you use to upload files to your web-space. If your web-hosting is anything like mine, then you will have the option to create your own user accounts (probably using PHPMyAdmin or similar). I reccomend that if you can you create a separate login for the counter.

$password - Same as for $dbusername

$databse - This is the name of the database that contains the counter information. You need to create this yourself, contact your system administrator if you do not know how to do this.

$imagepath - This is where the counter will look for the images to display. This can be a relative path to images in your webspace, or an absolute path to a location on another website (get permission from the site owner before you do this).

Once you have made those changes, save the file and upload it to your webspace.

The images

In the previous section, you specified where the images would be stored. You now need to make sure that location exists, and then put the following images there:

0.gifPictures of the digits 0 - 9
1.gif
2.gif
3.gif
4.gif
5.gif
6.gif
7.gif
8.gif
9.gif
left.gifLeft and right borders for the counter. If you don't want borders, then just create 0x0 pixel images.
right.gif
x.gifThe image that will be loaded if the counter value could not be read

I chose to use gif images, as they uses lossless compression, and support transparency. If you really don't want to use gif images, then find the getImage() function, and change the value of the $extension variable to the extension of your choice.

Creating/Editing the counter(s)

For your convenience (and mine), I have written a couple of pages that will allow you set up and manage the counters on your site. Once you have all of the information required above, go to the counter admin page and enter your login details. From there, you can create, remove and edit your counters.

Note: Earlier, I said that the host will be localhost if the database is on the same site as your web page. This is still true, but don't forget that the admin page is running from my site, so using localhost as the host won't work, you need to put in the address of your site.

Using the counter(s)

If you have never used PHP before, all you need to know is that PHP is an ordinary html file, with a few extra commands available. PHP files also have a different extension to html files (.php rather .html surprisingly enough). You can only use the counter from a php file (unless you really know your stuff), so if you want to add the counter to an existing page, you will need to change the file extension from .html to .php, but don't worry, it shouldn't mess up the rest of you page.

Next, place the following at the top of your page somewhere:


<?php

include("counter.php");

?>

This tells the web server that you want to include the text from "counter.php" in this file, like a virtual cut-and-paste. Don't forget to change the name if you called your file something else.

Next, go to where you want the counter to be displayed, and enter the following:


<?php echo(XXX("counterName")) ?>

You now have a few choices. You need to replace the XXX bit with the function that provides what you want to see. Your options are as follows:

pageHit - gives you the value of the counter as text. (2461)

getPageHitNumber - same as pageHit. (2458)

getPageHitImage - gives you the HTML tags for image of the counter. ([2450])

observeCounterNumber - gives you the value of the counter as text, but doesn't increment it. (5)

observeCounterImage - gives you the HTML tags for image of the counter, but doesn't increment it.
([5])

Don't forget to change "counterName" to the name of the counter you created.

That's it. If you have any problems, my contact details are at the bottom of the page.