ASPit - Totally ASP JSit - Totally JavaScript
Search PHPit

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

Advertisements

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\">\"$image\" alt=\"Ad Banner\" style=\"border:0;\" width=\"468\" height=\"60\" />');";

// 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.

« Previous: Creating the ad system



4 Responses to “Building an advertising system with PHP, Part 1”

  1. PHPit - Totally PHP » Building an advertising system with PHP, Part 2 Says:

    […] var site=”s20phpit” « Previous Building an advertising system with PHP, Part 1 […]

  2. PHPit - Totally PHP » Building an advertising system with PHP, Part 3 Says:

    […] 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. […]

  3. mudkicker.com » banner y�netim sistemi Says:

    […] 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. […]

  4. » Building an advertising system with PHP - James Silvester’s Blog Says:

    […] Stage1 […]

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 & Dynamic JS
  2. Creating the ad system
  3. Frequency Cap & Conclusion
Bookmark Article
Download Article
PDF
Download this article as a PDF file