Low End Mac's Online Tech Journal

Counting Clicks with PHP and MySQL

Website Automation with PHP and MySQL, Part 11

Dan Knight - 2002.06.28

Last week we created our database for external links and figured out how to display these links even when fields were empty, as covered in Part 9 and Part 10 of this series. Now we take things a step further.

I've been including external links on the Low End Mac home page since April 1997, the month I began the site. In all these years, I've had no way of knowing how often visitors to the site follow those links or which links have been the most popular.

Working with PHP and MySQL to manage external links, I now have the opportunity to track how many times a link is clicked. I'll be able to use that information to better understand what visitors to the site find interesting.

Counting Clicks

The first step in counting clicks was creating a PHP script that would use the unique ID of each record in our database, increment the count for that record, and then open the desired page. Here's the core of that code:

$array = mysql_fetch_array(mysql_query("SELECT URL,clicks FROM xlinks WHERE ID='$id'"));
$clicks = $array[clicks] + 1;
mysql_query("UPDATE xlinks SET clicks='$clicks' WHERE ID='$id'");

header("Location: $array[URL]");

This finds record X in the xlinks (external links) database, reads the URL and number of clicks, adds one to the click count, and then opens the desired page with the last line of code.

Of course, this means we have to go back and modify our display code so that it calls up this PHP script (portal.php) instead of leading directly to the external page. Where the earlier script contained this line:

  <a href=\"$array[URL]\">$array[linktext]</a>, ";

the revised script reads thus:

  <a href=\"/scripts/portal.php?id=$array[ID]\">$array[linktext]</a>, ";

When I click on a link, it quickly calls up the portal script, increments the count, and launches the desired page. It happens so fast that you'll probably never notice the difference, but now I'll be able to track how often these external links are followed.

Learned From Tracking Clicks

Since implementing this system, we've posted 60 external links. The most clicked outside article was Crouching Apple, hidden "Jaguar" on Extreme Tech, which received 438 visits due to our link. Right behind it was A battle PC giants should lose on osOpinion at 347.

The least clicked external link was Apache anti-hack patch posted, which was only relevant to those running their own servers on OS X or some other flavor of Unix, with 27 clicks.

Overall, we're typically seeing 60-200 clicks to the articles we link to. After all these year, it's nice to know how often these links are being used.

Join us on Facebook, follow us on Twitter or Google+, or subscribe to our RSS news feed

Today's Links

Recent Content

About LEM Support Usage Privacy Contact

Follow Low End Mac on Twitter
Join Low End Mac on Facebook

Favorite Sites

Cult of Mac
Shrine of Apple
The Mac Observer
Accelerate Your Mac
The Vintage Mac Museum
Deal Brothers
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ


The iTunes Store
PC Connection Express
Macgo Blu-ray Player
Parallels Desktop for Mac

Low End Mac's Amazon.com store


Open Link