A Simple Banner Rotator Script

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[&apos;randnum&apos;])
	{
	$randnum = mt_rand(0,$count);
	}
$_SESSION[&apos;randnum&apos;] = $randnum;
echo &apos;<a href="&apos;.$links[$randnum].&apos;"><img style="border:none" src="&apos;.$images[$randnum].&apos;" /></a>&apos;;

?>

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 😀

You can see an example of the script running here.

This entry was posted in Coding and tagged , , , . Bookmark the permalink.

13 Responses to A Simple Banner Rotator Script

  1. Don Morris says:

    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. papa_face says:

    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 😀

  3. Luke says:

    Do you have an example of this running??

  4. papa_face says:

    I have updated the post to contain a link to the script 🙂

    Here.

  5. papa_face says:

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

  6. Jauhari says:

    Thanks for this wonderful script, I will use it

  7. VegBang says:

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

  8. papa_face says:

    I’m glad you like it 🙂
    Thank you for commenting.

  9. raul says:

    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. eligio says:

    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. Michael says:

    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. papa_face says:

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

  13. Pingback: My Homepage

Leave a Reply