This is a little PHP script I wrote to rotate ads, text snippets, images, web pages, links and anything else rotatable. In fact, I have two instances of it running in JournalXtra – one to display random ads in the sidebar and another to display random links in the sidebar. I’ve found it so incredibly useful that I thought I’d better write about it and share it.
Affectionately called Randy, the PHP script performs a simple task: it reads an external file, picks a few random lines from it and serves them to a web browser. It is intended to be used to randomly display text ads within web pages but can easily display any HTML content.
I’ve used it to display
- random hyperlinks
- random images in a gallery
- random tube clips
- random user reviews
- random ads
- random text snippets
- and more…
It is easy to use, just
- download the script (randy.php)
- upload it to your server
- unzip it
- create a text file called ads.txt
- put the content to be displayed by Randy into ads.txt
- call the script within your web page
The randy.php file and the ads.txt file should be stored in the same directory as the page that calls randy.php. For example, if the random ads, images or text snippets are to display in index.php then the randy.php and ads.txt files should be stored in the same directory as index.php.
Each item to be displayed must be on its own line within ads.txt. There must not be any empty lines and you can include any HTML formatting you like. Any HTML formatting must be within the same line as the item it affects. For example, if you have data for three ads in ads.txt and you want each item to be contained by a <div> box and for any text to display in bold then that must be stated in each line, viz
<div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="http://some-place-one.com" /><br /><a href="http://some-place-one.com"><strong>Click Here</strong></a></div> <div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="http://some-place-two.com" /><br /><a href="http://some-place-two.com"><strong>Click Here</strong></a></div> <div style="margin-left: auto; margin-right: auto; text-align: center;"><img src="http://some-place-three.com" /><br /><a href="http://some-place-three.com"><strong>Click Here</strong></a></div>
Additional CSS styling through element IDs and Classes are observed when the random items are rendered within a browser.
Call the script with either a PHP include:
<? include("randy.php");?>or a Server Side Include:
<!--#include virtual="randy.php"-->
The web page that calls the script should be an .shtml or .php file type (it will not work for a .html page).
The script comes with a few options, namely
- $title — the title to display with ads (optional)
- $file — the file that contains the ads or data to display randomly
- $pre — anything that should display before any random data is displayed (optional)
- $post — anything that should display after any random data is displayed (optional)
- $infix — anything that should display between each randomly displayed item (optional)
- $show — to maximum number of ads to display
- $per_row — ads, images, data may be displayed in columns. Use this to specify the number of items to display per row
I usually leave $title blank. Use a <br /> for the spacer ($infix) if you want each item displayed on a line of its own or use if you want each item displayed on the same line (in a row) but separated with a single space. Any symbol or HTML may be used as a spacer e.g <hr /> or |. Alternatively, specify the number of items to display per row and have Randy autogenerate a table to display the items in.
Randy also has two advanced settings:
- $companion — this is for specifying a file containing companion data to display alongside the main file data
- $infill — specifies the data to use when the companion file has fewer datalines than the primary file
- $dpath — this is for specifying a directory path to the datafiles when they are located in a different directory to the page that calls randy.php
The companion file is ideal for specifying textual information to display next to graphical ads. Think along the lines of the way Facebook ads display. To achieve this effect, place your graphical ads in the primary datafile ($file) and each graphical ad’s companion text in the secondary datafile ($companion); ensure both parts (graphical and textual) occupy an equal line number (as would be apparent in a non word-wrapped document). When called to display, the ad images will appear to the left of their text. To display the text first, put the text in $file and the graphics in $companion.
When the companion file contains fewer datalines than the primary file, the companion file is appended with enough lines of data to ensure both files have an equal number of datalines. The appended data will be either empty newlines else whatever is stated in $infill.
Randy can be used in WordPress by placing randy.php and ads.txt in the root directory of your WordPress installation (same place as index.php) and calling it with a PHP widget or PHP short tag. The ads served here at JournalXtra are served by Randy.










Thanks a lot for sharing the important free web scripts of php.
Thanks for posting David, it’s my pleasure to write about the tools, scripts and knowledge that I find useful when I develop websites. Keep visiting because there are more to come :)
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
That’s good to know. I used to use OpenX and other ad plugins but they’re more hassle than they’re worth so now I just use a PHP script to randomly display ads and maybe some time soon I’ll use the autorotation script in conjunction with the PHP script. I do know that my clickthrough rate has increased since I began randomly rotating my ads, and that has to be good, right?
Thanks for visiting, Techtips, look forward to reading you here again :-)
Thanks for sharing this free web scripts of php .I was searching it from so many days.
Thanks a lot!!
You’re welcome :)
Your tips are very good for rotating ads.
Please keep sharing content like this i will be in touch with you.
Thank you Carol. I have more articles planned. I will release one today and another one next week. They’re not about rotating ads but I will update this article over the coming months to include the new tricks I have learned.
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.
It’s always nice to know when I have helped someone. Thank you for posting. Remember to backlink to JournalXtra for future reference and to help others find us more easily :-)
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??
It’s not easy to make a suggestion without looking at your actual code. I suspect you’ve referenced the incorrect files in either the php or html. If you want me to look at them, paste their contents into separate text files on your server and give me links to them either here or through the contact form; and I will do what I can for you.
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.
You’re welcome Kelly.
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 :)
thanks
Very good script. Thanks a lot.
Thanks! After one hour searching on Google I finally found what I was looking for.