Skip to page content or skip to Accesskey List.
Search evolt.org
evolt.org login: or register

Work

Main Page Content

How to count clicks in your website

Rated 3.91 (Ratings: 1) (Add your rating)

Log in to add a comment
(3 comments so far)

Want more?

 
Picture of pedrito

peter van dijck

Member info | Full bio

User since: October 22, 1999

Last login: August 30, 2005

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?url=evolt.org&id=3">
instead of
<a href="http://evolt.org">
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: http://evolt.org"); 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?url=evolt.org You'll be sent straight to evolt.org. 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:

 id INT AUTO_INCREMENT NOT NULL PRIMARY_KEY, clicks INT

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')";
 $result=mysql_query($sql,$db);
 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.
  • poorbuthappy.com/ease Weblog
  • petervandijck.net Portfolio
  • Easytopicmaps.com
  • liga1.com Accessibility and localization
  • Submitted by pedrito on February 4, 2000 - 22:22.

    Here's an example of how you can use it with some surprising results: what navigation is being used?

    login or register to post comments

    Submitted by deboute on February 5, 2000 - 06:49.

    everyday, i'm stricken by new 'nifty' (© pedrito) php features !
    will that madness ever end ?!

    login or register to post comments

    One added not about REGISTER globals in PHP...

    Submitted by cezz on January 15, 2006 - 14:54.

    As more and more people aswell as more and more server companies are starting to become more aware of the DANGER resgister_globals are when on people are now disabling them, infact for many versions now php has been shipped with register globals set as OFF... So where am i Going with this? Basicaly the code you supplied wont work with Register_globals set as off because you have called the GET variable from the URL using a server variable, this can be fixed in two ways1. simply add $url = $_GET['url']; before the header("loc... OR change the line  header("Location: http://$url"); /* Redirect browser to web site */ To  header("Location: http://$_GET['url']"); /* Redirect browser to web site */ This is just my peice on the matter but many scripts are now failing due to a lack of consideration to register_globals change...

    NEW CODE!!!

    login or register to post comments

    The access keys for this page are: ALT (Control on a Mac) plus:

    evolt.orgEvolt.org 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.