Nice and simple click counter

phool4fool

203.***.***.***
2,038 days ago

Nice and simple click counter

The code provided is for a page, clicks.php, which when referenced retrieve the amount of clicks for banner and then update the this amount and store it back in the database before sending the user along to the actual link. The db is also pretty simple, with a unique "bannerID", description text, URL, clicks count and timestamp. You could use something like this to link to clicks.php:


echo "<a href=\"clicks.php?id=".$arrayRow['bannerID']."\" target=\"_blank\"><img border=\"0\" width=\"468\" height=\"60\" src=\"images/banners/".$arrayRow['banner']."\" alt=\"".$arrayRow['site']."\"></a>";

PHP Example: (!)

PHP code:

<?php
// connect to the database

include('includes/connect.php');

// grab the ID from the URL
$bannerID = $_GET['id'];

$query = "SELECT url, clicks FROM banners WHERE bannerID='$bannerID'";

$result = mysql_query($query)
or die(mysql_error());

// grab the number of clicks and the url that we're linking to
$arrayRow = mysql_fetch_array($result);
$clicks = $arrayRow['clicks'];
$url = $arrayRow['url'];

// add one to the number of clicks
$clicks++;

// update the db with the new number of clicks
$update = "UPDATE banners
SET clicks = '$clicks'
WHERE bannerID = '$bannerID'";
mysql_query($update)
or die(mysql_error());

// close the connection to the DB
include('includes/close.php');

header('Location: '.$url.'');
?>