|     |     |  

How to Make a PHP Banner Ad Rotator Script

Have you ever visited a website that showed you the same exact ad on every single page you visited? You quickly put it in your mental "IGNORE THIS" list, which made it basically invisible.

A much more effective way to display ads on your website is to show a different ad every time your visitors refresh the page or visit a new page. If you hit refresh on this page, you should see a different advertisement at the top of the page - because I'm using a banner ad rotation script written in PHP.

Here's How:

PHP has a shuffle function that randomizes (shuffles) the order of the elements in an array. Creating a rotating banner ad is just a matter of using this function to shuffle a list of ad scripts.

1. Define your banner ads

Create a $banners array with the links of each banner. Then shuffle this array.

1
2
3
4
5
6
7
8
<?php
$banner1 = '<a href="//rickysays.com/BANNER1_LINK">
    <img src="//rickysays.com/BANNER1_IMG_SRC" /></a>';
$banner2 = '<a href="//rickysays.com/BANNER2_LINK">
    <img src="//rickysays.com/BANNER2_IMG_SRC" /></a>';
$banner3 = '<a href="//rickysays.com/BANNER3_LINK">
    <img src="//rickysays.com/BANNER3_IMG_SRC" /></a>';
 
$banners = array($banner1, $banner2, $banner3);
 
shuffle($banners);
?>

2. Print the shuffled banner ad

To show the actual banner ad, print the first item in the banner ad array (which will be different every time the page is loaded or refreshed, thanks to the shuffle function.

1
2
3
<div>
  <?php print $banners[0] ?>
</div>

You can put any combination of text, images, and links inside each $banner variable, and you can include as many $banner variables as you want.

Just be sure to include all of the banner variables in the array on line 9 above.

You Might Also Like