Monday, January 21, 2008

Adrotator in PHP

Creating adrotaror is a simple step in PHP.

You will have to create two files, the source from which you want to load the advertisements and the source which will read the ads to load on your page.

You can create the ads.txt with following contents:


First Ad
--
Second Ad
--
Third Ad


Replace the text First Ad, Second Ad, Third Ad with your Ad text. You can ad unlimited number of ads as long as you split them with a seperator (here -- acts as a seperator)

Then create a rotator.php file with the following contents:

<?php
$filecontent = join ('', file ('ads.txt'));
$adarray = split("--",$filecontent);
$selectedad = rand(0,(count($adarray)-1));
echo $adarray[$selectedad];
?>



Thats it. You will have a random ad displayed wherever you include

<?php include "rotator.php" ?>

2 comments:

vast said...

But this logic is giving me error, saying that txt file not exists... coz while reading the path is considered including the random.php

advisor said...

when you run the file on the page... it should show you the path where it shows the file is not found. Just put your txt file in the same path & you should be good.