How to Create and Use a PHP Banner Rotator

Knoji reviews products and up-and-coming brands we think you'll love. In certain cases, we may receive a commission from brands mentioned in our guides. Learn more.
How to create and implement a php banner rotator script in your Website.

One of the many php scripts that I use, and one of my favorites, is a php banner rotator. I use the banner rotator to automatically change the ads of products that I sell on my Website. Each time the page is reloaded a new ad appears. This keeps your page fresh with new content, plus it gives your ads maximum exposure. This script can also be used as an image rotator or for displaying text.

Below is the code for the php banner rotator that I created and use on my Website. Feel free to copy and use this script if you like. To show how the code works the example below has only two banners. You can add as many banners to the rotator as you like just make sure that the rand number, which means random number, is equal to the number of banners. For example, if you have 6 banners, the rand number should be $num = rand (1,6,);

The first thing you need to do is copy the banner images into the image file in your root directory. If you are using affiliate code you can place the affiliate code in the rotator also. Be sure to place the image source <img src=""> and  URL <href> in the right places.

Next fill in your image location, your alternate text and the url of the link.


$Img1 = "http://www.(the source of the image)";

$Alt1 = "[advertisement] (place your alt text here)";

$Url1 = "http://www. (the url of the link)";

$Img2 ="";

$Alt2 = "[advertisement]  web development";

$Url2 = "";

$num = rand (1,2);

$Image = ${'Img'.$num};

$Alt = ${'Alt' .$num};

$URL = ${'Url'.$num};

Print "<a href=\"".$URL."\"><img src=\"".$Image."\" alt=\"".$Alt."\" /</a>"; ? >

This php banner rotator validates with W3C.  Save this banner rotator script in your include file of your root directory and use the include function to display it on your site. <?php include ("banner-rotator.php"); ?>. When you need to make changes to the script, add or remove banners, you simple make the changes to the script in your include file and the changes appear throughout your Website.

I hope you find this script useful.