Random PHP Banner Rotation Script
I wrote this script which is used to rotate different types of banners. So far this script can rotate: images, swf, and code. The code will handle any code put into it so be careful when testing. /** /** * Simple Banner Rotation Script * @author Jeremy Simkins aka SoN9ne * * $i is used instead of a number to make the script easier to manage without having to renumber each array item. * $i can be replaced with a hard coded number, its a personal prefrence - Make sure each array item has its own number or else this will fail */ // Define Vars $bannerPath = 'http://www.siteurl.com/banner_path/'; // Path to folder with banners
/** * Build Banners Array * * Banner Types Supported: ((type) - banner fields used) - (type is required for the script process) * (code) - type, code * (img || image) - type, image, href (optional - target, title, alt) * (swf || flash) - type, src, width (optional - height, href, target, title) */ $i=0; // Counter
// Sample IMAGE Banner $banner[$i]['type'] = 'image'; // Banner Type - Required $banner[$i]['image'] = 'image.jpg'; // Image with extension - Required $banner[$i]['href'] = 'http://www.site.com/'; // Link - Required $banner[$i]['target'] = ''; $banner[$i]['title'] = ''; $banner[$i]['alt'] = ''; $i++; // Increment Banner ID
// Sample SWF Banner $banner[$i]['type'] = 'swf'; // Banner Type - Required $banner[$i]['src'] = 'swf_file.swf'; // Banner name with extension. Required. $banner[$i]['width'] = '800px'; // SWF Width - Required $banner[$i]['height'] = ''; $banner[$i]['href'] = 'http://www.site.com/'; // Link - Optional, should be in the swf $banner[$i]['target'] = ''; $banner[$i]['title'] = ''; $i++; // Increment Banner ID
// Sample CODE Banner $banner[$i]['type'] = 'Hello World!'; // Banner Type - Required $banner[$i]['code'] = 'test'; $i++; // Increment Banner ID
// Display Banner - Do not edit below here unless you know what you are doing. showBanner();
/** * Displays a Random Banner * * Will check to make sure the banner is valid before it is shown, if it fails it will reload another banner till it find one that works. */ function showBanner() { global $bannerPath, $banner;
// Fetch random key $rn = array_rand($banner);
// Check for type: if fail, reload another banner if (!isset($banner[$rn]['type']) || !$banner[$rn]['type']) { showBanner(); return; }
// Show Banner switch ($banner[$rn]['type']) { case 'code': case 'js': if (!$banner[$rn]['code']) { showBanner(); return; } echo $banner[$rn]['code']."\n"; break;
case 'flash': case 'swf': if (!isset($banner[$rn]['src']) || !$banner[$rn]['src'] && !$banner[$rn]['src'] || !isset($banner[$rn]['width']) || !$banner[$rn]['width']) { showBanner(); return; } $banner[$rn]['src'] = $bannerPath.$banner[$rn]['src'];
// Are we using a link or is this done properly within the SWF? if (isset($banner[$rn]['href']) && $banner[$rn]['href']) { echo ''."\n"; }
echo ''."\n"; echo ''."\n"; echo ''."\n"; echo ''."\n"; echo ''."\n";
// Now we have to the anchor if its used if (isset($banner[$rn]['href']) && $banner[$rn]['href']) { echo '."\n"'; } break;
case 'image': case 'img': default: if (!isset($banner[$rn]['image']) || !$banner[$rn]['image'] && !$banner[$rn]['href']) { showBanner(); return; } echo ''."\n"; break; } }
Let me know what you think, or any improvements.
September 11 2009, 11:09pm | Original Link »

