Low End Mac's Online Tech Journal

Counting Clicks with PHP and MySQL

Website Automation with PHP and MySQL, Part 11

Low End Mac Reader Specials

Memory To Go Special: New 2008 iMac 2GB $42 / iMac Intel Core2 DUO & MacBook Pro 2GB $36 - 1GB $20. MacPro 8 Core Memory 8GB kit $286 / 4GB kit $143 / 2GB kit $93 -- Free shipping available. LIfetime warranty.

Download Typestyler, still the Ultimate Styling Tool for Internet, Print and Video Graphics. Works great in Classic with a Native OS X Version on the way. Free Tryout: www.typestyler.com

LA Computer Company: Specials on AppleCare, iMac's, Apple Batteries and Apple A/C Adapters. Also Great prices on Used Apple Computers. Call 1-800-941-7654 Click Here.

OWC: NewerTech NuPower Batteries for iBook and PowerBooks Designed+Built in USA to run longer, LAST LONGER TOO! Free Battery Recycling Return Label; Quality High-Capacity from $99.95

Mac users can finally play Party Poker for Mac. Not only that, they can also learn how to play PokerStars for Mac.

Laptop Hardware Provided by TechRestore - Overnight Mac & iPod Repairs.

Compare products like desktop computers, laptops, and LCD TVs side by side! All the information and reviews to make the best purchasing decision for a new cell phone GPS products or MP3 players. The Ciao network makes searching products easy for you.

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.

Recent Online Tech Journal Columns

Links for the Day

Recent Content on Low End Mac

Go to the Online Tech Journal index.

About LEM | Support | Usage | Privacy | Contacts


Have a question?
Ask an expert!

Navigation

Used Mac Dealers
Apple History
Best Used Macs
Video Cards
Email Lists
InfoMac's Low
End Mac Forum

Favorite Sites

MacSurfer
MacMinute
MacInTouch
MyAppleMenu
InfoMac
Macs Only!
The Mac Observer
Accelerate Your Mac
RetroMacCast
PB Central
MacWindows
The Vintage Mac
   Museum

DealMac
DealsOnTheWeb
Mac2Sell
ramseeker
Mac Driver Museum
JAG's House
System 6 Heaven
System 7 Today
the pickle's Low-End
   Mac FAQ

Abandonware
   Petition

Mac vs. PC Info

Affiliates

The Apple Store
Mac Connection
MacMall
TechRestore
MacResQ
ExperCom
Crucial Memory
batteries.com

Advertise

Open Link