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()

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.

SEO Friendly Redirects with PHP

Filed under: PHP,SEO — Tags: , , — Adi @ 1:32 pm January 24, 2009

With a basic redesign you probably wont have this problem but if you have rebuilt a website even with the same content if the URL’s have changed this will have a huge effect on your search engine presence as you will be starting from scratch. Your links from internal pages may not work, links from external sites wont work and you will lose your pagerank (if it ever mattered anyway.)

A good way to keep your sites listing after a redevelopment is to use a redirect. If you are using an Apache server you can use the .htaccess file to redirect.

Redirect 301 /oldpage.html /newpage.html

But some times this isnt the best way if you have to redirect hundreds of pages. The alternative is to use a header redirect in PHP. Most web based languages such as ASP or RUBY will do the same thing but with different code.


<?php

// make this code the first line
header("Location: /newpage.php",TRUE,301);
// 301 Moved Permanently to newpage.php
?>

« Newer PostsOlder Posts »

Switch to our mobile site