1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Simple Banner Rotation

Very Simple Banner Rotation


The simplest way to display random banners on your site is to use the rand () function and assign each image and link a number. The banner that corresponds to the random number generated is then displayed.

In this code we can assign an address for each image and a URL that it will link to. In our demonstration we have three banners, but you can use as many as you like, just follow this format.

 $Img1 = "http://yoursite.com/image.jpg";
 $Url1 = "http://www.identity.st";

$Img2 = "http://www.hissite.com/bannerA.gif"; $Url2 = "http://www.about.com";

$Img3 = "http://hersite.com/MyImage.gif"; $Url3 = "http://php.about.com";

Next we will generate a random number and then build variables based on it. We set rand to (1,3) however, if you choose to have more or less banners you need to change 3 to the exact amount you have.

 $num = rand (1,3); 
 $Image = ${'Img'.$num}; 
 $URL = ${'Url'.$num}; 
Finally we print out the banner and link it to the appropriate URL.
Print "<a href=".$URL."><img src=".$Image." border=0></a>";
The full script can be downloaded here.

1.)To make one banner appear more than others, assign it to more than one number, so it is twice as likely to be called.
2.) Try using include () to add your banners to other pages.

  1. About.com
  2. Technology
  3. PHP / MySQL
  4. Step By Steps
  5. Web Banner Advertising - Banner Display - Random Banner

©2014 About.com. All rights reserved.