You Are Here: Home »Tutorials»Php_mysql »   Click tracker Friday September 3rd 2010

Click Tracker

Introduction

This is not going to be a great tutorial in terms of explaining how things are done, i wrote this quite quickly when somebody requested it on a forum and i'm only really posting it this way because i'm too lazy to tidy up the code and make it a good enough script to download.

The main thing is though, it'll work, so i'm sure with a little editing you can have something worth using, or mabye just learn from it.

You could use this as the basis for alot of things where you want to display the number of clicks to each link, a list of affiliates, a download script, anything like that.

Create The Table

First we need to create a table in our database where the link counts will be recorded, hopefully you have phpmyadmin or some other program you can run this query from to create a table.

CREATE TABLE links (
id INT (10) not null AUTO_INCREMENT,
name VARCHAR (50),
url VARCHAR (50),
clicks VARCHAR (50) not null DEFAULT '0',
PRIMARY KEY (id)
);

The Database Info

Here we'll just create a small file to hold our database info that we can reference to in the script, obviously you need to change the details below to your own, then save this as 'vars.php'.

<?php

$db_host 
"localhost";   //should be fine as it is
$db_user "dbusername"//your database username
$db_pass "dbpassword";  //your database password
$db_name "dbname";   //the database name

?>

The Add-Link Page

Now we need a page that we can easily add new links from, so first of all create a new directory call it linksadmin or something like that, save the folowing as 'index.php' and upload it to that new directory, then also upload your 'vars.php' file to the same directory.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
    "http://www.w3.org/TR/XHTML1/DTD/XHTML1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
<head>
<title>Links Admin Page</title>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<?php
// get the database info
require 'vars.php';

//connect to the database
$db mysql_connect($db_host,$db_user,$db_pass);
      
mysql_select_db($db_name);

//if the form is submitted
if($_POST['submit'])
{
//insert the new link into the database
$sql[postlink] = mysql_query("INSERT INTO links (name, url) VALUES ('$name','$url')";);

//close the connection
mysql_close($db);

//confirm the link was added
echo "$name ($url) has been added\n";

/* !! note !! there's no error checking here to see if
the query was succesfull, it would be advisable to add some */

}
?>
</head>
<body>
<table>
<tr>
 <td style="text-align:right">
 <form name="addlink" method="post" action="./">
       Link Name: <input type="text" name="name" />
       <br />
       <br />
       Url: <input type="text" name="url" value="http://" />
       <br />
       <br />
 <input type="submit" name="submit" value="Add Link!" />
</form>
</td>
</tr>
</table>
</body>
</html>

Now, obviously you don't want other people to be able to randomly add links to your site and also you should never keep files with sensitive information (usernames, passes etc) in publicly accessibile directories so you will want to protect that linksadmin directory somehow, i'd suggest using this password protection method.

The Counting Script

You don't really need to edit this part, unless you made any changes to the create table code and changed the table or field names, if not just save it as count.php in your main directory.

<?php

//get the database info
require '/linksadmin/vars.php';

//connect to the database
$db mysql_connect($db_host,$db_user,$db_pass);
      
mysql_select_db($db_name);

//get the target link from the url
$link $_GET['link'];

//select the link from the table
if(!empty($link))
{
$sql[getLink] = mysql_query("SELECT url, name FROM links WHERE id = '$link'");
while(
$data mysql_fetch_array($sql[getLink]))
{
      
//update the count for that link by 1
      
mysql_query("UPDATE links SET clicks=clicks+1 WHERE id = '$link'");

}

//close the database
mysql_close($db);

//finally, redirect to the link
header("Location: $data['url']");

}

?>

Displaying The Links

Finally, you can either save this as a seperate page, say links.php or use the code from within it to display your links and their individual counts on your own pages, this would also need to be in your main directory unless you change the path to vars.php in the first line.

<?php

//get the database info
require '/linksadmin/vars.php';

//connect to the database
$db mysql_connect($db_host,$db_user,$db_pass);
      
mysql_select_db($db_name);

//fetch all the links
$sql[getLinks] = mysql_query("SELECT id, name, clicks FROM links ORDER BY id");
while(
$data mysql_fetch_array($sql[getLinks]))
{
//print out the list of links
echo "<a href=\"count.php?link=$data[id]\">$data[name]</a> - $data[clicks]\n<br />\n<br />\n\n";
}

//close the database
mysql_close($db);

?>

That's it, not pretty or packed with features but it works as you can see from this example so even if you tear it apart and just use the SET clicks=clicks+1 query as a basis for your own well at least you gained something =)

Some things you might want to do if you're developing this as a stand alone script:

  • Add a login for the addlinks page
  • Add an option to edit and delete links
  • Perhaps add an option for different categories