ASPit - Totally ASP JSit - Totally JavaScript

PHPit - Totally PHP

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 1 out of 3)

Introduction

Most of us who run websites will most likely want to add advertising on our websites some time in the future. The easiest way to do this is to simply take the advertising code (be it HTML or JavaScript), and paste it directly into the code of our HTML. But what if we eventually get multiple advertisers for the same spot? In the beginning this is unlikely, but as your website grows and becomes more popular, it's to be expected that you will have accommodate for multiple advertisers.

In that case you must use some sort of advertising management script. The best one around, phpAdsNew, is completely free and can be downloaded at http://www.phpadsnew.com, but this might be slightly overkill for most websites, since phpAdsNew is a complete ad management application, packed with features you probably won't need anyway. Why don't we create our own simple advertising system?

In this article, the first part of a new three part series, I will show you how to build your own advertising system. In this first part I will take you through the basics of dynamic JavaScript, and how to serve multiple advertisements in the same spot. Let's get started.

Dynamic JavaScript using PHP

The easiest way to serve different advertisements is to use an external JavaScript file, e.g.

<html>
        <head>
                <title>Demo 1</title>
        </head>

        <body>

        <script src="ads.js" type="text/javascript"></script>

        </body>
</html>

The ads.js file can insert any content using document.write (or by adding new elements to the DOM). This means we can use JavaScript to serve different ad's in the same spot, but why do all the backend work in JS, when we've got PHP to work with?

First of all, point the script tag to a PHP file, like this:

<html>
        <head>
                <title>Demo 1</title>
        </head>

        <body>

        <script src="js1.php" type="text/javascript"></script>

        </body>
</html>

Then paste the following in js1.php:

<?php

$time = date("g:i:s A", mktime());
header ("Content-Type: text/javascript");
echo "document.write ('The time is now $time');";

?>

(View Live demo)

This simple example demonstrates that it's possible to use JS and PHP together to add dynamic content. The js1.php uses JS to output extra content, with can contain anything.

Let's have a look at creating a really bare-bones advertising system now.



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