RSS

A Simple Banner Rotator Script

This entry was posted on Jan 02 2008

I'm in the coding mood today :) .
I cannot count how many times I've been asked to code a UBER simple banner rotator script.
These things are so simple, but if you don't know PHP it can be difficult to understand.

The script below has two variables:

$links = array(
"http://www.google.com",
"http://www.msn.com",
"http://forums.digitalpoint.com"
);
//add a new link for the banner to a new line in the same format as above
 

and:

$images = array(
"http://www.google.co.uk/intl/en_uk/images/logo.gif",
"http://stc.msn.com/br/hp/en-us/css/35/decoration/msn_b.gif",
"http://forums.digitalpoint.com/images/misc/dps_logo.gif"
);
//add a new image link for the banner to a new line in the same format as above
 

As you can see they match up. The first line for the $links variable is for a google image, the second is for a msn image etc.
Here is the full code:

<?php
session_start();

$links = array(
"http://www.google.com",
"http://www.msn.com",
"http://forums.digitalpoint.com"
);
//add a new link for the banner to a new line in the same format as above

$images = array(
"http://www.google.co.uk/intl/en_uk/images/logo.gif",
"http://stc.msn.com/br/hp/en-us/css/35/decoration/msn_b.gif",
"http://forums.digitalpoint.com/images/misc/dps_logo.gif"
);
//add a new image link for the banner to a new line in the same format as above
//-------- DO NOT EDIT BELOW THIS LINE----------
$count = count($links) -1;

$randnum = mt_rand(0,$count);

if      ($randnum == $_SESSION['randnum'])
        {
        $randnum = mt_rand(0,$count);
        }
$_SESSION['randnum'] = $randnum;
echo '<a href="'.$links[$randnum].'"><img style="border:none" src="'.$images[$randnum].'" /></a>';

?>
 

The code has been constructed so that the likelihood of getting the same banner displayed twice is greatly reduced.

Comment here if you like the script, or have any questions :D

You can see an example of the script running here.

[Slashdot]
[Digg]
[Reddit]
[del.icio.us]
[Facebook]
[Technorati]
[Google]
[StumbleUpon]


12 Responses to “A Simple Banner Rotator Script”

  1. Hi,

    Thanks for this script. It is very simple, but might just fir the bill for me.

    I had to replace the apostrophes (single quotes) in the echo statement for it to work. Those slanteed ones created a parse error.

    echo (‘‘);

    also had to take the spaces out of the style attribute. My just be my hosting plan.

    See it here: http://donhmorris.com/sbr.php

    (Same links as script; no change.)

    Okay, thanks.

    Warmly,

    Don :)


  2. Thank you for highlighting that problem. It appears to be a wordpress issue. I will try to sort it out :)
    Thank you for coming to my blog :D


  3. Do you have an example of this running??


  4. I have updated the post to contain a link to the script :)

    Here.


  5. Just an update. I have fixed the quote problem, so people can copy and paste code from this site without problems now :D


  6. Thanks for this wonderful script, I will use it


  7. Minimalistic and superb! Copypasted and worked just out-of-the-box. Thanks for sharing.


  8. I’m glad you like it :)
    Thank you for commenting.


  9. Thanyou so much, i’d like if you have something exactly like this that can be integrated into a wordpress plugin. Again, thanks for this piece of “gold code”


  10. is it possible to create a javascript version of this script ? i have a gamesitescript which is using cache function, so php don’t work. thanks


  11. Hi,great code.works perfect.
    What can i add to the code so,
    when the banner is clicked , the browser
    will open up in a new window. Prefer to
    have my site and the link open at the same
    time. thank u kindly


  12. Hi,
    Simply add target=”_blank” to your href code.


You must be logged in to post a comment.