Skip to page content or Skip to Accesskey List.


Main Page Content

How To Count Clicks In Your Website

Rated 3.91 (Ratings: 1)

Want more?

  • More articles in Code

peter van dijck

Member info

User since: 22 Oct 1999

Articles written: 23

An absolute beginners tutorial for total niftyness in PHP!

Hi. What if you could count the amount of times visitors clicked on certain links on your site? You could track how often they click on that link to greenpeace, or how often they click on the links at the top of the page compared with those on the bottom of the page. You could even count how often they click on that animated gif you had set up as a link, or how many people you're sending to other sites.

Well, I'm here to tell you: it's easy! This is an Absolute Beginners™ tutorial.

What you'll need for this tutorial:

  • PHP
  • mySQL
  • a good kick in the butt (we all need that now and then)

What we'll make:

We'll make a click.php3 page, through which all clicks will go. It will log where the click came from and put that in a database. So your links will look like
<a href="/click.php3?">
instead of
<a href="">
where id will tell the click.php3 page where the link came from. This way, you can have a bunch of links all have the same id, and we keep it simple. Simple, as you know, makes us Happy.

Tricky bit: no more relative links like this: ../../page.html (We're keeping it simple)

Ok, let's get started. There's a nifty function in PHP (look it up) that's called header. It sends a header. It can be used to send the browser to another page, like this:
<?header("Location:"); exit;?>
Try it out, upload it and go to the page. You'll be send straight to evolt.

Tricky bit: be careful not to put any spaces before the header function. This will force output and it won't work.

Cool hey? Now try this:
<?header("Location: http://$url"); exit;?>
Can you guess what this does? Upload it, and then go to it like this: click.php3? You'll be sent straight to Niftyness!!!

Now you can see the possibilities here, right? We're gonna need a database to track all this counting going on. Good thing you've got mySQL installed!

Now, the following bit will be easier if you've done some databasing before. But if not, don't worry, you can do it! To make all this really easy, you can use a database interface like facemysql, available for free. Or you can do it all from the command line.

We'll set up a table in our database that looks like this:
name: clickcount
fields(=columns): id / clicks
Yes, only 2 fields! (remember the Simple thing?)

To set up the database: we'll call the table clickcount:


That's all. Now you need to make a bunch of rows and set the clicks all to 0(id=1,clicks=0; id=2,clicks=0; id=3 ... and so on). You can do that easily with facemySQL.

Have the database set up?

Good! That was the hard part. Here's the complete program, have a look at it:


//connect to dbase

$db=mysql_connect("hostname","user","pass"); // username and password

mysql_select_db("pedro",$db); // pedro is name of database

// clickcount is the table, with 2 columns: id and clicks

$sql="UPDATE clickcount SET clicks=clicks+1 WHERE (id = '$id')";


header("Location: http://$url"); /* Redirect browser to web site */

exit; // just to be nice


Short explanation:

First, we update the database. And then, we send the user to the page he's going to for continued happiness in surfing.

And that's that for today. Cool hey?

Peter Van Dijck is an Information Architect with an interest in localization, accessibility, content management systems and metadata.
  • Weblog
  • Portfolio
  • Accessibility and localization
  • The access keys for this page are: ALT (Control on a Mac) plus: is an all-volunteer resource for web developers made up of a discussion list, a browser archive, and member-submitted articles. This article is the property of its author, please do not redistribute or use elsewhere without checking with the author.