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.
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:
Then paste the following in js1.php:
$time = date("g:i:s A", mktime());
header ("Content-Type: text/javascript");
echo "document.write ('The time is now $time');";
?>
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.
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 […]