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!

Notify of

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

Newest Most Voted
Inline Feedbacks
View all comments
Would love your thoughts, please comment.x