PHP Link Rotator, PHP Link Rotator Script


 

PHP Link Rotator, PHP Link Rotator Script

This script is useful for those who have no database access. Use just a few lines of this PHP code to output a random banner or random link into your HTML page.

This script is useful for those who have no database access. Use just a few lines of this PHP code to output a random banner or random link into your HTML page.

PHP Link Rotator

In this tutorial I'm going to learn you own to create a simple rotation script. It can be used for rotating banners or for your links page. You don't need to have database access, it is just one php file.

Ok let's start with some info.

How would I do rotating ?


I need an array containing something to output and I need to get elements in a random order form that array to show them on the page. Here we create an array:

$codes = array(
    "<a href=roseindia.net><img src=banner1.gif></a>",
    "<a href=newstrackindia.com><img src=banner2.gif></a>",
    "<a href=javajazzup.com><img src=banner3.gif></a>"
); br>
It should contain the set of all possible codes to show on the page.

Next we should randomly get one element and show it on the page. This part can be done in numerous ways:
1. First way is to fetch an element with a random index from array. But in this case when we get a random index we need to check if we already outputted that element or not.
2. Second way is to fetch an element with a random index and then delete it from an array so we don't have to check everytime when getting new random value.
3. Third way is to sort array in a random order and just output its contents.
II think the most easy way is third, so let's build the script !

To sort array in a random order I use these two lines: code>

srand ((float) microtime()*1000000);
shuffle($codes);

First line written above will seed random values generator and the second will sort an array randomly. To get better results it is recommended to place srand function as far as it is possible from the line that gets a random value. We will do it in the final script further. And the last part of the script will output records on the page:

while(list(, $code) = each($codes)) {
      echo "$code";
}

You can use this simple rotating mechanism for placing links, banners, or just any text. I used the code shown below to output two of three possible links on the page:


<html>
<body>
Check out these links:<br>
<?
$codes = array(
&    "<a href=roseindia.net><img src=banner1.gif></a>",
    "<a href=newstrackindia.com><img src=banner2.gif></a>",
    "<a href=javajazzup.com><img src=banner3.gif></a>"
);br>
shuffle($codes);
$i=0;
$number=2; while(list(, $code) = each($codes)) {
       if ($i>=$number) { break; }
       echo "$code";
       echo '<br>';
       $i++;
}
?>
</body></html>

OOf course this script can't show you the number of views for each link. But with some cool modifications (writing the number of views into a file) you can create your own advanced banner rotation software and sell it to someone.

Ads