ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

Use this textbox to search for articles on PHPit. Seperate keywords with a space.


Building an advertising system with PHP, Part 3

(Page 1 out of 3)


Welcome to part 3 of the "Building an advertising system with PHP" series. In the previous parts (part 1 and part 2) I have shown you how to build your own advertising system using PHP and JavaScript. We've also added two extra features to our ad system and in part 2 we built a page to manage the ads as well. If you haven't read either part yet, I highly recommend doing so before reading this part.

In this part, the final part of the series, I will show you how to first track all kinds of statistics on each ad, and after that display neat graphs using PHP/SWF Charts. Let's get started.

Gathering Ad Statistics

Gathering ad statistics is actually quite easy, and all we need to add is some simple database code to the ad.php file. But first, let's add a new table to the advertising database, which looks like this:

CREATE TABLE `traffic` (
  `trafficid` mediumint(10) NOT NULL AUTO_INCREMENT,
  `datetimestamp` int(10) NOT NULL DEFAULT '0',
  `type` enum('click','view') NOT NULL DEFAULT 'view',
  `ipaddress` varchar(255) NOT NULL DEFAULT '',
  `ad` mediumint(10) NOT NULL DEFAULT '0',
  PRIMARY KEY  (`trafficid`)

We only store a few basic things, like the IP address of the visitor and the current date/time. This table will be used for both views and clicks, hence the need for the type field. All we need to do now is to add some simple code that will log a view. Copy and paste the code below somewhere in the ad.php file (if you don't know what the ad.php file is or don't have it yet, download the demo source of the previous part).

// Insert new view into traffic table
$datetime = time();
$ipaddress = ss($_SERVER['REMOTE_ADDR']);

$db->query ("INSERT INTO traffic (ad, datetimestamp, type, ipaddress) VALUES ({$ad['adid']}, $datetime, 'view', '$ipaddress')");

Now all the impressions of each ad are being logged, but the clicks aren't logged yet. To be able to log those, we have to use another script that will log the click, and then redirect the visitor to the URL of the ad. The script looks something like this:

include 'mysql.php';

// Get ad id
if (isset($_GET['id'])) {
        $id = intval($_GET['id']);
} else {
        die('No ad specified');

// Get ad
$ad = $db->query_first ("SELECT adid, link FROM ad WHERE adid = $id");

if ($ad == false) {
        die('No ad specified');

// Log click
$datetime = time();
$ipaddress = ss($_SERVER['REMOTE_ADDR']);

$db->query ("INSERT INTO traffic (ad, datetimestamp, type, ipaddress) VALUES ({$ad['adid']}, $datetime, 'click', '$ipaddress')");

// Redirect
header ("Location: " . $ad['link']);

All this script does is first check whether the ad exists, and if it does, logs the clicks and redirects the visitor to the ad link.

We also have to customize the ad system to use the redirect script instead of using the direct link, like so:

// Display ad:
header ("Content-Type: text/javascript");
echo "document.write ('\"redir.php?id={$ad['adid']}\">\"$image\" alt=\"Ad Banner\" style=\"border:0;\" />');";

The ad is now linked to our redirect script (redir.php) instead of the direct link. Now that all the traffic is being gathered, we have to write the next part of the statistics package: the analyzing part.

Next: Analyzing the traffic »

Leave a Reply

About the author
Dennis Pallett is the main contributor to PHPit. He owns several websites, including ASPit and Chill2Music. He is currently still studying.
Article Index
  1. Introduction & Gathering Statistics
  2. Analyzing the traffic
  3. Creating the graphs & Conclusion
Bookmark Article
Download Article
Download this article as a PDF file