#native_company# #native_desc#

A Small Rand Banner System

By PHP Builder Staff
on April 17, 2012

Imagine that you had just built your site in PHP and now you want to improve it adding some random banners which, if pressed, bring the user to the sponsor site. If you don’t have the necessary resources to use a professional database like MySQL, PostgreSQL or Oracle, you can do a simple randomized system of banner using text files to keep the banners/URLs, and write this little PHP code to sort the banners. It is a start!

The first step is to create the banners file. Here we’re going to show the structure of the file:


The first column is the address of the site, the second column is the address of the image in your server and the third column is the text that appears when the users roll the mouse over the image (hint). The character | will delimit the beginning of each field.

So we have the file banners.txt

http://www.uol.com.br|uol_banner.gif|content about brazil
https://phpbuilder.com|php_banner.gif|all around php
http://www.hotmail.com|hot_banner.gif|free email address

We have 3 banners registered in this file. We won’t build a program to manage the inclusion/exclusion of the banners in this file, but you can do a cool one. Be careful (remove) the white line at the begin and the end of the file.

Let’s now write the file banner.php3:


function PrintBanner() {
$fp = fopen(“banners.txt”, “r”);
$index = 0;
while (!feof($fp)) {
  $line[$index] = fgets($fp, 256);

$rIndex = mt_rand(0, $index -1);
list($img, $url, $alt) = explode(“|”, $line[$rIndex]);
$banner =  “<a href=”$url” target=_blank> <img src=”$img” border=”0″ alt=”[$alt]”></a>n”;
return $banner;


Now you have to include this file in the PHP page that you want and then build the call to the function PrintBanner()


include (“Banner.php3”);
// your code here
echo PrintBanner();
// your code here


This program uses the method File System. If you want to obtain a complete reference about each one of then access: http://www.php.net/manual/ref.filesystem.php

 -o) Fernando F Torres                      Programador WEB
 / [email protected]       
_|V Belo Horizonte – MG
                             $so =~ s/”Windows”/”Linux”/ge;