This script can be pasted into any .tpl file. See demo at thinktechno.com.

From http://drupal.org/node/48908

Ad File Format

Place code for each ad in a text file. Write "~" between ads.

~
ad code
~
ad code
~

Template Script

Place this script in your .tpl files where you want ads to appear. Specify the ad file you want the script to use, for example files/ads.txt.

$fcontent = join('', file('files/ads.txt'));
$s_con = split('~', $fcontent);

$banner_no = rand(0, count($s_con) - 1);
echo $s_con[$banner_no];

Notes

  • The script will randomly display ads from the ad file.
  • Any type of ad is supported: Adsense, flash, banner, etc.
  • If you use a 468x60 banner as a header ad, all ads in that ads.txt file should 468x60. Other dimensions, like 120x600, will get corrupt your page layout.
  • You can use different ads in different parts of your page. To do so, create multiple ad files and specify the path to the one you want to use in the script for that part of the page.

Alternative for Multiple Ads

This code will let you have multiple ads randomly, as well as prevents duplication of items. Note that you MUST have at least the same number of ad entries in your ads.txt file that you define as being output (this example is for 8 ads, just add/delete the echo statements to change the number you would like output):

<div align="center">
<?php
srand((float) microtime() * 10000000);
$fcontents = join ('', file ('files/theme_editor/gunmetal/ads2.txt'));
$input = split("~",$fcontents);
$rand_keys = array_rand($input, 8);
echo $input[$rand_keys[0]] . "\n";
echo $input[$rand_keys[1]] . "\n";
echo $input[$rand_keys[2]] . "\n";
echo $input[$rand_keys[3]] . "\n";
echo $input[$rand_keys[4]] . "\n";
echo $input[$rand_keys[5]] . "\n";
echo $input[$rand_keys[6]] . "\n";
echo $input[$rand_keys[7]] . "\n";
?>
</div>