How to rotate ads or affiliate banners?

If you have many ads or affiliate banners and you want to rotate them on your website, here is the way on how to do it. But make sure your server supports PHP because we are using PHP script here.

First, you need to create a blank PHP file and name it as rotateads.php. Then copy this code and paste it in.


$bannerCounter = 1;

$bannerCode[$bannerCounter] = ‘replace with your ad code‘;

$bannerCode[$bannerCounter] = ‘replace with your ad code‘;

$bannerAdTotals = $bannerCounter – 1;
mt_srand((double)microtime() * 1234567);
$bannerPicked = mt_rand(1, $bannerAdTotals);
$bannerPicked = 1;
$bannerAd = $bannerCode[$bannerPicked];

echo $bannerAd;


As you can see at the above code, you need to replace “replace with your ad code ” with your own ad or banner code. If you want to put more ad in the rotation, just add these two more lines

$bannerCode[$bannerCounter] = ‘replace with your ad code‘;

below the previous one.

To include the rotateads.php in your theme file, you need to call this function. I assume you have uploaded the file in your root folder.

<?php include(“/home/yourname/public_html/rotateads.php”); ?>

Make sure to put real path to your website or it might not work. I was trying using the link ( but it does not work on my new server. But when I use the real path, it is working without any problem.


  1. can we use Google adsense code?

  2. I was using the same trick but it does not increase your CTR. so better if you put your adsense just like that. no need to rotate it.

  3. Hi, this is unrelated, you may remove this comment after reading it.

    You introduce ImageFly on your site a year ago and I joined the program through your referal. It was good at the begining but I have prolem when I request my payment from them.

    I was then joined another program called PicsPay, a similar program like them ut the minimum pay is $10.


  4. how to show different banner within the same time?

  5. I think you need to create two PHP files then. 🙂

  6. Hello!

    I think this php script looks pretty cool!
    I would like to use it.

    However I have a few questions:

    1) I think the example you made for us above, is for rotating only 2 banners.

    If I had 10 Banners I wanted to rotate, what should I do?

    Just add more of these right?
    $bannerCode[$bannerCounter] = ‘replace with your ad code‘;

    I think that is what you were saying.

    Do I need to change any other values in the code?
    I don’t see anything with a value of 2…

    2) I would like to align four Ad rotations side by side.

    Would I be making 4 php files, then place them like this:



    Will this make my four adds line up side by side?
    I don’t want them in rows one on top the other.

    3) Also, can I simple put “Spacer Images” between each ad?
    Like to keep them 10px apart from each other.
    I would just put my image between each right?

    4) This one is not necessary, but is there any way to change the frequency at which certain ads in the php file appear?

    Are their appearance rates equal and purely random?
    Or can they be altered individually?

    Thanks allot for Providing us with this code!
    I’m excited to try it out soon!

  7. 1. Just copy the code and replace the text with the image html code.

    2. Just call the file in your template 4 times. No need to create 4 PHP files.

    3. You can use CSS for that. Create class for the image.

    4. It is rotate randomly.

    Hope these help.

  8. Yup! I had fixed everything already!
    I got it working!

    Thanks allot for the reply, and this cool script!

    All the best!

  9. Hi what do you mean by “real path”. How do I get the real path? I’m confused.

    You wrote:

    But where do I put my URL or root folder? What do I call it?

  10. Will this work in a static HTML page by the way? I can’t seem to get it to work.

  11. It only works in PHP because that script was written in PHP.

  12. Thanks a lot. finally I’ve found the solution for my problems. You re great. 🙂

  13. Simple script thank you very much