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

Creating the ad system

Our ad system will pull it's advertisements from an array that looks like this:

// ### Advertisements:
$ads = array(
                        // Array (link to website, image url)
                        array('http://phpit.net', '../phpitbanner.gif'),
                        array('http://www.aspit.net', '../aspitbanner.gif'),
                        array('http://www.google.com', '../468x60banner.gif'),
                        );

As you can see it's very easy to understand, and all we need is two things: a link and an image url.

What our ad system must do now is pull a random ad from the above array, and display it using JavaScript. The code to do this looks something like this:

// ### Advertisements:
$ads = array(
                        // Array (link to website, image url)
                        array('http://phpit.net', '../phpitbanner.gif'),
                        array('http://www.aspit.net', '../aspitbanner.gif'),
                        array('http://www.google.com', '../468x60banner.gif'),
                        );

// Get a random ad
$adkey = array_rand($ads, 1);

// Get ad
$ad = $ads[$adkey];
$url = $ad['0'];
$image = $ad['1'];

// Display ad:
header ("Content-Type: text/javascript");
echo "document.write ('\"$url\">\"$image\" alt=\"Ad Banner\" style=\"border:0;\" width=\"468\" height=\"60\" />');";
?>

If you now run the above code in conjunction with the ad page, which looks like this:


       
                </span>Ad System (Part 1)<span class="sc2"><span class="kw2">
       

       

        src="ad1.php" type="text/javascript">

       

(View Live Demo)

You will notice that random ads are displayed. We've now got a very basic, but functional advertising system, using nothing more than some simple PHP and JavaScript.

« Previous: Introduction & Dynamic JS
Next: Frequency Cap & Conclusion »



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