Skip to main content

How to Create your Own PHP URL Rotator

Leanore has been a self taught web master since the age of 12. She's learned a lot of neat tricks over the years and she'd like to share!


What is a PHP URL Rotator?

A URL Rotator is a single URL or link that you can distribute that will direct visitors to one of a list of possible landing pages. It's a great tool for promoting multiple pages and posts at a time by passing along one simple Link.

It's something that sounds simple. However, it's a difficult enough task that I was totally stumped when I first decided to create my own PHP Rotator.

The Fun Button I added to my site that directs the visitor to a random page of content.

The Fun Button I added to my site that directs the visitor to a random page of content.

What do you Use a Rotator For?

A PHP Rotator has a multitude of uses. As a webmaster, it will easily turn into one of your most valuable marketing tools. The primary function is to drive traffic to all of the pages listed in your rotator script.

Examples of Where to Use your Rotator Link:

  • In your Ads - Using a Rotator Link as your promoted link when you purchase advertising will allow you to drive traffic to all of the pages on your site you want seen. Without having to pay for multiple links to be advertised.
  • Traffic Exchange Sites - There are mixed reports as to how effective or harmful using a traffic exchange site can be for your website. As many of these sites restrict how many links you can promote, using a rotator can spread the traffic across the pages on your site.
  • Fun Buttons! - This is something I did on my Website. At the bottom of the home page content, I added a "View Something Random" button. I figure if it's someone's first time to my site, they can use it to pick what they read first at random. It's something fun, and gets clicked more than you'd think!

Creating your Rotator

There are a few steps to be able to get your Rotator Fully Functional. It's not as complicated as you might think. You don't really have to be a coding expert, especially with me here to help you.

example of my folder for my rotator script.

example of my folder for my rotator script.

Step 1. Create your Rotator Directory

For this, you'll need to access your website's file manager via the CPanel. Alternatively, you can also directly access your site's files with an FTP client. However you choose to connect, you'll need to create a folder. You can name this folder whatever you like, but remember that the folder name will be the end of the URL you are sharing. Something simple like, "rotator", usually works fine.

The index.php file in the "rotator" folder.

The index.php file in the "rotator" folder.

Step 2. Create a .php File in your Rotator Folder

Next, you're going to want to create a file to put in the folder you just created. For now, just creating a file named "index.php" and placing it in the folder is good. We'll get to what the content of that file should be in a little bit.

We are naming it "index.php" in order to ensure that the script is automatically loaded when someone visits your url. If you named your rotator folder "rotator", your URL will look something like " ". You don't have to add the "index.php" at the end because it's name will make it the default home page for that link.

Sounds confusing, but it works. Trust me!

Scroll to Continue
An Example of what an index.php page for a rotator looks like.

An Example of what an index.php page for a rotator looks like.

Step 3. Create your PHP Rotator Script

This is the part that probably feels the most intimidating. However, I am going to be giving you some pretty simple "cut & paste" codes you can use to get going. The most you'll have to worry about is making sure that your URLS are all entered correctly.

You can copy the code below and paste it into your 'index.php' file. You can use a text editor for this, or the code editor in CPanel. Either way works. Just make sure your URLS are all entered correctly.

You can add as many URLS as you like to the PHP Code. Just copy the last line and paste it, then update the URL. You can see by the example image of the code I am using, that you can add quite a few with ease.

Example Code for Copying


$urls[] = "Your First URL Here";
$urls[] = "Your Second URL Here";
$urls[] = "Your Third URL Here";

srand  ((double) microtime( )*1000000);
$random = rand(0,count($urls));

header ("location:" . $urls[$random]);

You're All Done!

That wasn't so painful was it? If my instructions weren't too complicated, you should have your very own functional PHP URL rotator!

You won't have to worry about some Traffic Exchange site putting a big ugly banner across the top of your pages in order to promote multiple links at a time. The number of links you can promote with your script is unlimited. You can even create more than one rotator to keep the types of links your marketing seperated if you like.

Now that you've got the code working, what are you waiting for? Go test it out and have fun with it! The Possibilities are endless!

If you have any questions, don't hesitate to comment or contact me.

© 2017 Leanore King


Martin on July 12, 2019:

seems not to work with the most recent php

Leanore King (author) from Austin, TX on June 08, 2017:

It can be used to link to a button. Once you have it set you can share or hyperlink the url how you like :-)

Universitas Widyatama from Bandung on June 07, 2017:

Oh I see, that means it's random button with php, right? Thanks for information

Related Articles