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:
"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:
"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:
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
You can see an example of the script running here.


1-4-2008 17:37:45
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
1-4-2008 17:40:54
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
1-4-2008 21:39:40
Do you have an example of this running??
1-4-2008 21:47:27
I have updated the post to contain a link to the script
Here.
1-5-2008 01:09:35
Just an update. I have fixed the quote problem, so people can copy and paste code from this site without problems now
1-6-2008 21:50:32
Thanks for this wonderful script, I will use it
1-8-2008 19:28:53
Minimalistic and superb! Copypasted and worked just out-of-the-box. Thanks for sharing.
1-9-2008 22:45:30
I’m glad you like it
Thank you for commenting.
1-24-2008 18:42:18
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”
2-21-2008 01:58:13
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
4-24-2008 14:36:56
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
4-24-2008 21:40:04
Hi,
Simply add target=”_blank” to your href code.