Building an advertising system with PHP, Part 1
(Page 3 out of 3)Creating a frequency cap
Often you might want to limit a certain advertisement to a certain number of impressions, i.e. an ad only gets 1000 impressions and not more because the advertiser paid for only 1000. Our ad system can't do that yet, but we can certainly build it in.
To be able to limit an ad, the system must log each view of an advertisement, which means we must write to a log file. To keep everything as simple as possible, our ad system will simply write to a log file every time an ad is viewed like.
The new code for the ad system looks like this:
// ### Log file
$logfile = 'log.txt';
// ### Advertisements:
$ads = array(
// Array (id, link to website, image url, impressions)
array('phpit', 'http://phpit.net', '../phpitbanner.gif', 5),
array('aspit', 'http://www.aspit.net', '../aspitbanner.gif', 5),
array('gen', 'http://www.google.com', '../468x60banner.gif', 1000),
);
// ####### Don't edit below ##########
$logpath = dirname(__FILE__) . '/';
$logfile = $logpath . $logfile;
// Open log file
$log = array();
if (file_exists($logfile)) {
$log = file_get_contents($logfile);
$log = unserialize($log);
// Update imps
for ($i = 0; $i < count($ads); $i++) {
$ad =& $ads[$i];
if (!isset($log[$ad['0']])) continue;
$ad['3'] = $log[$ad['0']];
}
}
// Which ads have no impressions left?
$temp = array();
foreach ($ads as $a) {
if (intval($a['3']) > 0) {
array_push ($temp, $a);
}
}
$ads = $temp;
// Get a random ad
$adkey = array_rand($ads, 1);
// Get ad
$ad = $ads[$adkey];
$url = $ad['1'];
$image = $ad['2'];
// Display ad:
header ("Content-Type: text/javascript");
echo "document.write ('\"$url\">\"
// Decrease impressions
$ad['3'] = $ad['3']-1;
$log[$ad['0']] = $ad['3'];
// Serialize log
$log = serialize($log);
// Write log
$f = fopen($logfile, 'w');
fwrite($f, $log);
fclose($f);
?>
What this code does is first get the log file, and get the updated number of impressions for each ad (with each having a unique id). After that it sorts through the ads array, and removes all the advertisements that are at zero impressions, because they shouldn't be shown anymore. Finally it displays the advertisement, decreases the impressions count by one, and writes the new log file.
This code will count down till zero, and will then stop displaying the ad, and thus we have built in a frequency cap. Simple and messy, but it does the job!
Conclusion
In this first part of the 'Building an advertisement system in PHP' series I have shown you a really simple way of creating an ad system for your website. The above code is still very messy, and not ready for production use at all, but it was more of an introduction, and in the next part we'll start creating some beautiful code.
In the next part I will move away from the simple approach, and show you how to run our ad system from a MySQL database. We will also add two new features to our ad system (priority ads, and different types), and we'll be creating a simple admin page for the ad system.
If you would like to discuss this article, hop on over to the PHPit Forums or use the comments below.
The demos from this article are downloadable by clicking here.
December 5th, 2005 at 12:14 am
[…] var site=”s20phpit” « Previous Building an advertising system with PHP, Part 1 […]
December 18th, 2005 at 9:19 pm
[…] 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. […]
December 19th, 2005 at 9:33 pm
[…] PHPit.net’te �ok g�zel bir reklam (banner) y�netim sistemi olu�turmak i�in 3 par�al�k bir makale serisi (1., 2., 3. b�l�m)yazm��lar. Kesinlikle �neririm, veritaban�, javascript vs. yi kullan��lar� ger�ekten bence hem k�sa hem efektif olmu� diyebilirim. �ok be�endim. […]
June 24th, 2006 at 11:34 pm
[…] Stage1 […]