CountMySQL is a very fast and efficient way to keep counters in a database (which is much quicker and more stable than "traditional" CGI counters).

These scripts also give administrators the ability to reset groups of counters, individual counters, and set any counter to a desired value.

Here are the problems that we have had historically with other counters that I am trying to solve with these scripts:
- Counters that are CGI or perl based cause another process(es) to fork, thereby taking up extra system resources and processor cycles from the web server. Since these scripts are based on PHP, the processing runs in the memory space which is already taken by httpd (assuming you're running PHP as a web server module).
- CGI and perl based counters are usually reset whenver the server is rebooted, CGI or perl libraries are updated, or the server crashes. These scripts use MySQL to store the counter values for a much more reliable count.
- CGI and perl based counters are difficult (at best) to reset, or set to a desired value. These scripts come with an admin directory that gives users the ability to reset all counters within a certain base URL, to reset individual counters, or to set a desired value of a counter - with no need to modify the html code and/or reload the web page(s) containing the counter.
- If a user wants a list of all of the counters on their site, they have traditionally had to create a table with links to each of the counters on that site. When they add or remove a new page or counter, they have had to recreate the HTML table to reflect the changes (VERY cumbersome when more than 10 or so pages are tracked!). The index.php3 file in the admin directory of this distribution automatically reports all active counters (and their count) in a given base URL.
- Counters traditionally have had zeros until you fill in all of the numbers, at which point your counter hits it's count limit - and you have to modify tour code (on each effected web page) to make the counter wider. Since these scripts create the counter image "on the fly" the width of the counter is based upon the actual numbers contained in the count. These counters were designed to handle numbers up to 9,999,999,999 (1 less than 10 billion)! If you need more space, all it takes is modifying two scripts to accommodate wider numbers. I suspect however, that if you have 10 billion users going to one page, you probably need something a bit more robust than a counter script!

These scripts require PHP, which can be found at:
http://www.php.net/

GD must have been compiled with PHP by using the --with-gd option at PHP compile time. You can obtain GD from:
http://www.boutell.com/

These scripts make use of MySQL, which can be found at:
http://www.mysql.com/
Altering the code for use with other databases should be very easy.

You need to make sure that the files that this code goes in have proper file extensions for running PHP3. If the files have an extension of .html, you can add the following to the directory's .htacces file:
AddType application/x-httpd-php3 html

Setup is fairly simple. Once setup is complete, implementation and maintenance are easy...

1) Create a MySQL database:
You will not have to modify the included scripts too much if you use these commands:

mysqladmin create Counters
mysql
connect Counters
CREATE TABLE Count (
Base VARCHAR(100) NOT NULL,
URL VARCHAR(100) NOT NULL,
Number BIGINT);
2) Unzip and Untar the distribution files:
In most Unix environments:
gunzip CountMySQL.tar.gz
tar xf CountMySQL.tar

In Winodws, use WinZip

This will leave you with a directory called "counters." Inside of the directory you will find a file called "README" a file called "count.php3" a directory called "admin" and a directory called "move" In the admin directory, you should have:
changeCount.php3
index.php3
reset.php3
resetOne.php3
resetOneResponse.php3
resetResponse.php3

In the move directory, you should have:
combined-counter.php3
combined-counter2.php3
counter.php3
counter2.php3

3) Move into the "admin" directory and make the following changes:
- changeCount.php3:
Replace "db.host" "db.user" and "db.pass" on line 2 with your database host name, username and password
- index.php3:
Change "_BASE_" on lines 5 and 8 to the base URL. For example, if you are counting hits at "http://www.bob.com/" this setting should be "http://www.bob.com"
- resetOneResponse.php3:
Replace "db.host" "db.user" and "db.pass" on line 2 with your database host name, username and password
- resetResponse.php3:
Replace "db.host" "db.user" and "db.pass" on line 2 with your database host name, username and password

4) Move into the "move" directory and make the following changes:
- combined-counter.php3:
Replace "db.host" "db.user" and "db.pass" on line 18 with your database host name, username and password
- counter.php3:
Replace "db.host" "db.user" and "db.pass" on line 3 with your database host name, username and password

5) If your PHP setup has an "include" directory (check the web server setup or "php3.ini" file), the combined-counter.php3 and counter.php3 files (from the move directory) need to go into it. If you do not have this directory, the counter.php3 file needs to be in every directory that has a file to be counted, and a copy of the combined-counter.php3 file needs to be in every "admin" directory that is used.

6) Move the combined-counter2.php3 and counter2.php3 files (from the move directory) into your web root directory. For example, if your web pages are serverd from:
/usr/local/http/htdocs
These files need to be in that directory. Do the same for any virtual domains that will be using the counters.

If you do not want these files in the web root directory, you will need to modify line 43 of counter.php3 and line 46 of combined-counter.php3 accordingly.

7) Examine the count.php3 file. This is the code that goes on each web page that is to be counted. Add the two lines of code to the page to be counted, replacing "_BASE_" with the base of the current URL and "_URL_" with the rest of the URL.

For example:
If this document resides at:
http://www.bob.com/smash.html
The code that belongs in this file will look like:

require ("counter.php3");
CountMe("http://www.bob.com","/smash.html");

It doesn't matter if the forward slash trails the domain or precedes the document path - as long as you don't have the slah in both places - and as long as you are consistent.

8) If you don't want people messing with your counters, password protect the admin directory. Consult your web server documentation for details.

If you have multiple users in one directory space or need to split your view results by directory, you can specify the "_BASE_" settings in the instructions above to reflect your needs. For example:
If your users have their personal pages at:
http://members.bob.com/username/

To give them their own counters, setup the _BASE_ variable to the following for the user 'jack'...
http://members.bob.com/jack
And for 'jill'...
http://members.bob.com/jill

This will allow you to give access to administrative functions to only people that should have such access.

For bug reports or feature requests join the support mailing list at: http://open.appideas.com/

These scripts were created by Christopher Ostmo of AppIdeas.com and are Copyright 1999, AppIdeas, Inc.

The latest version of these scripts and other MySQL and PHP utilities can be found at:
http://open.appideas.com/

Feel free to use, modify and distribute these scripts freely, but please leave this notice attached.

!!Let me know if you use these scripts - I like to see other people's creativity!!