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. LEM

Join us on Facebook. Follow us on Twitter.

Recent Online Tech Journal Columns

Links for the Day

Recent Content on Low End Mac

Recent Deals

About LEM Support Usage Privacy Contact

Custom Search

Share

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

Low End Mac Reader Specials

Quantcast

TypeStyler 11 is now in the Mac App Store!! -- Special Introductory Price of $59.95!! -- To Buy From The Mac App Store Click Here Now!! Or buy direct from Strider Software.

Quantcast

Quantcast

Quantcast

Poker Mac Don't install Parallels to play poker online! Poker Mac will show you how to download and install a native Mac poker and Mac Casino applications in minutes.

Quantcast

Quantcast

Quantcast

Quantcast

Favorite Sites

MacSurfer
Cult of Mac
Shrine of Apple
MacInTouch
MyAppleMenu
InfoMac
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac Museum
DealMac
Deal Brothers
Mac2Sell
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End Mac FAQ

Affiliates

Amazon.com
The Apple Store
The iTunes Store
PC Connection Express
GainSaver
Parallels Desktop for Mac
eBay

Low End Mac's Amazon.com store

Advertise

Open Link