Low End Mac Reader Specials

TypeStyler For Mac OS X is Now Shipping! Download The Free Fully Functional 60 Day Tryout at www.typestyler.com

Poker Mac Don't install Parallels to play poker online! Poker Mac will show you how to download and install a native Mac poker application such as Full Tilt Poker Mac.

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

Compare products like desktop computers, apple laptops, apple macs, and LCD Monitors side by side! All the information and reviews to make the best purchasing decision for new mobile phones, sat nav systems, or MP3 players. The Ciao online shopping community makes searching products easy for you.

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

Recent Online Tech Journal Columns

Links for the Day

Recent Content on Low End Mac

Recent Deals

About LEM | Support | Usage | Privacy | Contacts

Custom Search

Amazon.com

Navigation

Used Mac Dealers
Apple History
Video Cards
Email Lists

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
B&H
MacMall
TechRestore
ExperCom
Crucial Memory
batteries.com

Advertise

Open Link