This is the second part to yesterday’s post Blackhat Technique To Hide Your Affiliate Code Behind An Image/Video in regards to rotating your ads randomly on your site. Some of you might be interested in showing different ads on there site without cluttering your website with banner ads. To keep the clutter to a minimum, you can rotate your affiliate ads. If you are looking to rotate different affiliate banner ads or affiliate links then you can copy/past these scripts below to get started in just a few minutes. You can use one of these scripts on every page of your site if you like. The only disadvantage is the viewer will not see all of your affiliate banners, because these only show one at a time. When you refresh the page, you will see other banners. This is a great method if you are trying to use skyscraper banners on your site. Instead of having the entire side of your website filled with them, you can rotate through them. That is one thing that does drive me crazy, a website with very little content and a scrollbar that is a mile long to compensate for the skyscraper ads.
Let’s take a look at the first script, it is short and to the point. There are several ways to randomize your affiliate banners, however this way is very easy to understand for anyone. I could use array’s, but I feel that some readers will feel intimidated by seeing some of the PHP code. This first code is very easy to understand for a beginner, so lets have a look. Just remember to remove the PRE and CODE blocks before using them.
<pre><code>
<?php
$rand = rand(1,2,3,4,5);
if ($rand == 1)
print ‘Affilate script 1′;elseif ($rand == 2)
print ‘Affiliate script 2′;if ($rand == 3)
print ‘Affilate script 3′;if ($rand ==4 )
print ‘Affilate script 4′;if ($rand ==5 )
print ‘Affilate script 5;
?>
</code></pre>
The above script will rotate through 5 different things for you. You could input your affiliate javascript code or IMG and A HREF tags. If you only have 4, then simply remove the last 2 lines of
<pre>
<code>
if ($rand ==5 )
print ‘Affilate script 5;</code>
</pre>
and change the $rand string to (1,2,3,4) so it knows to only randomize between 4 numbers. What I suggest doing is putting this in a separate file, lets call it random.php and simply use the php include on any page you want it to go on.
<pre>
<code>
<?php include(”http://www.YOURSITENAME.com/random.php”) ?>
</code>
</pre>
So if you have this on 40 pages and you need to make a change to an affiliate, you only need to make the change in the random.php and not on the 40 pages. This saves you a lot of time and makes your job much easier.
Now the second method is a little more complicated and requires a MYSQL database. You will need to create your database and have a minimum of 2 rows. I called the table BANNERS and created 2 rows, url and image. You can create another row for your alt tag if you like, this way every banner that gets rotated with have alt text for each.
This code simply connects to the database of DBNAME, selects all ( * ) from BANNERS and then order’s it by randomly choosing one of the items to display. The “.$row[“url”].” and “.$row[“image”].” are used to select the data from those rows in the database and echo’s them into the url and img tag. You would still put this code in a separate file, like random.php above so you only have to make changes to one single file. This also allows you to use this on multiple websites while only using one database. The only change you would make is the server address, instead of having “localhost” you would put in the direct server address to where the database is stored. So if you have 20 websites and are looking to monetize them with affiliate banner ads, this would be a great solution for you as well.
<pre>
<code>
<?php
mysql_connect(”localhost”,”USERNAME”,”PASSWORD”);
mysql_select_db(”DBNAME”) or die(”Unable to select database”);$query = “SELECT * FROM BANNERS ORDER BY RAND() LIMIT 0,1″;
$result = mysql_query($query) or die(mysql_error());mysql_close();
while ($row = mysql_fetch_array($result))
{echo ”
<a href=’http://”.$row[”url”].”‘>
<img src=’http://www.YOURSITENAME.com/images/banners/”.$row[”image”].”‘ alt=”></a>/>
“;}?>
</code>
</pre>
As stated before, there are several ways to randomize data with PHP so I am sure you could write a shorter code or whatever. Please feel free to post your alterations in the comments below to show the readers other methods of ad rotation besides what I have listed here. I hope this answers question and/or gives you the reader some ideas for ad rotation for your website.
This article was originally written on SEMSpot.com, a Search Engine Marketing Blog.












