Free PHP Random Rotation Script

A little PHP script to rotate ads, text snippets, images, web pages, links or anything else that you want to rotate randomly. Affectionately called Randy, the Randy script reads an external file, picks a few random lines from it then inserts them into a web page at the point where randy.php is called for use. This was initially written to display text ads in random order but can easily display any HTML content.

I’ve used Randy to display

  • random hyperlinks
  • random images in a gallery
  • random tube clips
  • random user reviews
  • random ads
  • random text snippets
  • and more…


  • Random display of data stored within a data file
  • Display of mapped companion data alongside the data chosen randomly from the primary input data file.
  • Data is displayed within an HTML table.
  • The number of columns to display the data within can be configured
  • The number of data items to retrieve can be configured
  • Per item CSS classes e.g. class=”randy-1″ and class=”randy-companion-1″
  • Randy os easy to use.


  • Download the script (randy.php)
  • Upload it to your server
  • Unzip it
  • Enter the Randy directory and edit the configs in randy.php
  • Add data to data.txt
  • Add data to companion.txt (optional)
  • Call the script into your web page

When editing data.txt, put each item of data on its own line within. There must be no empty lines. You can use HTML if you want to.

For example, to display three ads within <div> tags with inline CSS you would enter markup like this into ads.txt:

<div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="" /><br /><a href="#"><strong>One</strong></a></div>
<div style="margin-left: auto; margin-right: auto; text-align: center;"><img  src="" /><br /><a href="#"><strong>Two</strong></a></div>
<div style="margin-left: auto; margin-right: auto; text-align: center;"><img  src="" /><br /><a href="#"><strong>Three</strong></a></div>

Call the script into pages with either a PHP include or a server side include.

PHP include:

<? include("/relative-path-to-randy/randy.php");?>

Server side include:

<!--#include virtual="/relative-path-to-randy/randy.php"-->


Configure the following variables in randy.php:

  1. $title — the title to display with ads (optional)
  2. $file — the file that contains the ads or data to display randomly
  3. $pre — anything that should display before any random data is displayed (optional)
  4. $post — anything that should display after any random data is displayed (optional)
  5. $infix — anything that should display between each randomly displayed item (optional)
  6. $show — maximum number of items to display
  7. $per_row — data can be displayed in columns. Use this to specify the number of items to display per row. This is the same as setting a column count.

Randy has three advanced settings:

  1. $companion — this is for specifying a file containing companion data to display alongside the main file data
  2. $infill — specifies the data to use when the companion file has fewer lines of data than the primary file. can be set to ‘use item count’ if you want to display each item’s row placement number within the data.txt file.
  3. $dpath — this is for specifying a directory path to the data files if they placed within a sub directory of Randy.

The companion file is optional.


Downloaded from GitHub

Sharing is caring!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

newest oldest most voted
Notify of
David Hussy

Thanks a lot for sharing the important free web scripts of php.


Thank you for this information.


i know other wp plugin for rotate ads, but with your tips i know the details and simple way how to rotate my ads


Thanks for sharing this free web scripts of php .I was searching it from so many days.
Thanks a lot!!


Your tips are very good for rotating ads.

Please keep sharing content like this i will be in touch with you.


Found this very helpful. now just working on making it work with each item as a marquee for a randomly generating marquee. any ideas you have would be appreciated!

Great tips and thanks for this script.
I can now display ads at random on any site just adding one line in a widget (WP).
If I need to change the ads displayed at any time on all the sites, I change one line in one file, located on the ‘central’ site. Perfect.

I’ve used this to successfully create a random banner link at the top of my blog. However, I’ve tried to use it for a set of css hover images and I can’t get it to work. The second instance (with a different name) keep showing the images from the first instance. So we see the little header banners at the bottom of the page and the other images don’t show up at all.

Any ideas??

That is what we suspected, but we can’t find any mention of the wrong text file in the php file. That’s why it’s a puzzle. I’ll try to send a copy. Thank you.


hi ,
thanks for this script . Works like a charm.
but i need help please.
I have a ads.txt files with many htlm code (image with link)
and i would like to have by example 4 row and 10 column .
how can i do please :)


Very good script. Thanks a lot.


Thanks! After one hour searching on Google I finally found what I was looking for.


thanks for the tutorials

not download sir


Exactly what I was looking for! It is escaping me somewhere but the ad number displays below each ad. How do I remove that?