Link Counter

This script uses PHP and a mySQL database to count the number of times that your visitors select a specific link on your web page. The script does not count the number of times that a page is viewed but rather counts the number of times that a specific link is selected. This makes it ideal for tracking advertising links or file downloads.

The script is free to use on any non-commercial web site that you may have provided that you keep the code exactly as written (the only entries that you may modify are in config.php). If you have a commercial website you may still use the script (subject to the same conditions) in return for a donation toward the cost of my maintaining this site. In either case a link back to this site would be appreciated.

How to Install the Script

The first step is to obtain the script.
(Yes the counter is installed on this link so I know how many copies of the script have been downloaded and you can see the script in action as you download it - completely invisible unless you view the page source or have a browser that displays the link destination when you hover over it).

Install all files provided onto your server into the same directory as the the destination page or download file. The script only allows you to reference files in the same directory in order to stop others from stealing your bandwidth by using it to count selected links on their own pages. If you use a multiple directory structure simply install the same config.php, db.php, and count.php files into each directory (you only need one with the instcount.php, countn.php, and counta.php files).

Edit config.php to configure the script for your own use.

  1. userid = the userid you will type in to access the count admin function.
  2. password = the password that you will type in to access the count admin function.
  3. dbuser = the userid that provides access to your mySQL database.
  4. dbpass = the password to match your dbuser access to the mySQL database.
  5. dbdir = the domain name that provides access to your mySQL database (usually - localhost).
  6. dbase = the name of your mySQL database that will contain the guestmap table.
  7. epp = maximum number of entries to display per page in the admin function.

Load the instcount.php page into your browser to create the count table in your database. This file should be deleted after the page has been viewed.

Linking Via the Counter

Change the link on the calling page to call the count script instead of directly calling the destination page or file. The destination page or file is passed as the file parameter and the tag in the database that contains the count is passed as the tag parameter. If no tag parameter is passed the script will not update a count. For example, to count downloads replace href="inc/" with href="inc/count.php?" or to count ad links replace href="mypage.htm" with href="count.php?file=mypage.htm&tag=ad1"

Simply change whatever links that you want to count in this way using a different tag value for each.

Viewing the Counts

You can display the count for a given tag on any PHP page by including the countn.php script into your page passing it the tag whose count you want to display. For example: <? $tag="ad1"; include "countn.php"; ?>

The code simply substitutes the count into that spot in the page so you might use it with appropriate text to produce a meaningful message like this:

The number of times that this script has been downloaded is: 1072

The counta.php page also allows you to view the counts associated with all of the tags that you use with the count script as well as providing a simple means to delete a tag (which effectively resets its count to zero). You must enter the userid and password that you defined in the config.php file to be able to access this page.