Your open source for Development and Design

Random Banner with PHP

Filed under: PHP — Tags: , , , , , — Adi @ 4:26 pm February 20, 2009

This is a quick tutorial which shows how easy it is to make a random banner script with PHP. We will use a PHP array of images which is populated by a given directory and select a random index from the array to be the banner.

For this example I have chosen to list the files from a directory as this will keep the script fairly dynamic without the need of a database. The script could be easily modified to use a database but we can come back to that later on if we need to.

To start off all you will need is a folder with several banners and a page to display them on.

$dir will be the directory you have created to grab the images from, it is important to make sure this folder only contains banners.
$banners will be the array of images found in the given folder.


 $banners = array();
  $dir = 'res/banners/';
  if ($handle = opendir($dir)) {
  while (false !== ($file = readdir($handle))) {
  if($file != "." && $file != ".."){
  $banners[] = $file;
  }
  }
  closedir($handle);
  }

  $rand = array_rand($banners,1);
  echo "<img src='".$dir.$banners[$rand]."' alt='banner'/>";   

The code above uses a few handy built in PHP functions:
opendir()
readdir()
array_rand()
print_r()

Fix Canonical URL with link rel=”canonical” in PHP

Filed under: SEO — Tags: , , , , , — Adi @ 2:32 pm February 15, 2009

Recently the major search engines have worked together to help fix one of the biggest problems in SEO, The canonical URL. With this quick fix all you need to do is add a tag to the <head> section of the page and search engines will use this as an anchor to index the page.

since it is still fairly new we don’t know how it will work in the long run and if there are any side effects such as the pagerank not transferring.

The new tag looks like this

<link rel="canonical" href="http://www.open-source-web.com/">

But nothing can be that easy, obviously with a large site you wouldn’t want to write that out for a million pages so here is the PHP code to make it work.


$thispage = "http://www.";
$thispage .= $_SERVER["SERVER_NAME"];
$thispage .= $_SERVER["PHP_SELF"];
if($_SERVER["QUERY_STRING"]){$thispage .= "?".$_SERVER["QUERY_STRING"];}

<link rel="canonical" href="<? echo $thispage;?>">

Shorthand boolean IF with PHP

Filed under: PHP — Tags: , , — Adi @ 1:39 pm February 4, 2009

This is a good alternative to using a basic IF condition. It basically fits the whole thing on a minimal line of code.

It is anĀ  easy way to compare two values and select the right one. There are a few ways to use this but the basic jist of it is to compare two values and if its is true use the first value after the question mark, or if the condition returns false use the second value.

echo $targetvar =  ($somevar == "thisVar" ? "itIsTrue" : "itIsFalse");


In the example above we can set $targetvar with the condition. it will compare the first two variables with whatever you use, more then less then etc and it will choose the correct value from the two after the question mark.

Switch to our mobile site