As promised, here is a full commented version of the script. It is not as clear as the previous explanation, but I tried my best to explain, if you have questions or comments, please feel free to IM me or E-Mail me.
adv_rsig.phpCODE
<?php
#----------------------------------------------\
#----------------------------------------------\
# ADVANCED RANDOM 4.13.06 ||
# SIGNATURE SCRIPT By: Boost ||
#----------------------------------------------//
#----------------------------------------------\
# SCRIPT INFO: ||
#----------------------------------------------\
# This script will generatea random ||
# signature (or image). If features 2 ||
# options: ||
# 1. Type in your images in an array ||
# 2. Put all your images in a directory ||
# and the system will read the images ||
#----------------------------------------------//
#----------------------------------------------\
# FUNCTIONS USED IN SCRIPT: ||
#----------------------------------------------//
# - requice ||
# - for and while loops ||
# - explode ||
# - isdir, opendir, readdir, closedir ||
# - arrays ||
# - header('Location: ') ||
#----------------------------------------------//
#----------------------------------------------\
# ABOUT: ||
#----------------------------------------------//
# Author: Boost (Akash Narkhede) ||
# Published At: ||
# http://www.pixel-designz.net/ ||
# Personal Website: ||
# http://www.hidden-dimensions.net/ ||
# AIM: boost0728 ||
# MSN: boost0728@gmail.com ||
# E-MAIL: boost0728@gmail.com ||
#----------------------------------------------//
#----------------------------------------------//
// Type in the file types you want to be allowed
// Include the '.' in front of the extensions, or it won't be recognized
$file_types = array(
'.gif',
'.jpg',
'.jpeg',
'.png',
'.bmp',
'.tiff'
);
// Set the path to same folder:
path = './';
// Set the DocumentRoot:
$document = explode('/', $_SERVER['PHP_SELF']);
// The above takes the full url of the script,
// and explodes it based on every / it finds and stores it as an array in $document
$path_use = '';
// Now destroy the Script-Name and create the $path_use variable:
for ($i = 0; $i <= (count($document)-2); $i++) {
if ($document[$i] != '') {
$path_use .= '/' . $document[$i];
}
}
// This code might look tricky but it's not
// It counts the $document array and subtacts 2 because the array starts at 0 (that's 1)
// You have to subtract another value to get rid of the last array value which will contain the script name (adv_rsig.php - that's 2)
// If the value is not blank, it will append it to the $path_use variable with .= and add a / in front of it
// If everything is blank, nothing will be added to $path_use, and it will remain as ''
// Add a trailing / at the end of $path_use:
$path_use .= '/';
// Set the url:
$domain = $_SERVER['HTTP_HOST'];
############# BEGIN SCRIPT - NO NEED TO MODIFY BELOW ############
// Select whether you would like the automated directory reading, where you upload your sigs to a folder, and the system
// will automatically take care of the rest
// Set variable choice to 'dir' or 'array' based on preference
$choice = 'dir';
// If you choose dir the system will take all the images in a specified directory, make sure they are the correct file type, and then put them in the sig changer
// If you choose array, you have to manually add the url's to the images (good if you have a generic images folder)
// Make $images an array
$images = array();
if ($choice == 'dir') {
// NOTE: If your images are IN THE SAME folder as your adv_rsig.php file, $dir_path = './';
$dir_path = 'images/sigs/';
// Make sure $dir_path exists
if (!is_dir($dir_path)) {
// If $dir_path doesn't exist, echo an error and terminate the script:
exit($dir_path . 'does not exist. Please use a valid directory.');
} else { // $dir_path must exist..
// Open the directory:
if ($dh = opendir($dir_path)) {
// Read the directory, this is the correct way to read the directory
// $file now contains an array of all the files in the array
while (($file = readdir($dh)) !== false) {
/* The WRONG way to loop over a directory is:
while ($file = readdir($handle)) {
echo "$filen";
}
*/
// Get the extension of the file:
// strrchr gets the last instance of the '.' in the variable file, obviously, the extension..
// strtolower lowercases the extension incase it is capitalized, ie. .JPG or .GIF, this is only to check the extension in the array
$ext = strtolower(strrchr($file,'.'));
/* The below if statement checks for a few things
1. Is $file the folder itself "."? If it is, we don't need it
2. Is $file the folder above (even if it is the root folder)? Again, we don't need it
3. Does the link to $file exist?
4. Is the file type extension ($ext) in the allowed array?
If all of the requirements are satisfied: add the file to the $images array, which will be used to get the random sig
*/
if ( $file != '.' && $file != '..' && is_file($dir_path.$file) && in_array($ext, $file_types) ) {
$images[] = $path_use . $dir_path . $file;
}# end if statement
}# end readdir($dh) while loop
}# end if( $dh = opendir($dir_path) )
// We are done with the directory now, so we can close it:
closedir($dh);
}# end is_dir($dir_path)
// Now that all the sigs are stored in the $images array, we need to display only 1 of the images in it:
// So...count up the array, subtract 1 from the count() because arrays start at 0, and forward the script to the signature
// We use rand(min,max) to select a random image
$get_sig = rand(0, count($images)-1);
$url = 'http://'.$domain.$path_use.$images[$get_sig];
header('Location: '.$url);
#### END DIRECTORY OPTION ####
#### BEGIN ARRAY OPTION ####
} elseif ($choice == 'array') {
// Require the sig_array.php file
// We use require instead of include because require returns a FATAL error and terminates the script while include only returns an error
require('sig_array.php');
// Count up the array, and forward to the signature
// Once again you -1 from count() becasue arrays start at 0
// And again...rand(min,max)
$get_sig = rand(0, count($images)-1);
header('Location: '. $images[$get_sig]);
#### END ARRAY OPTION ####
}
// If for some reason $choice is not 'dir' or 'array' nothing will happen
// To use this script in a forum use this code:
// [IMG][URL]http://www.yoursite.com/path/to/adv_rsig.php[/URL][/IMG]
// Enjoy!
// If you have any questions/comments see the table at the very top for contact info :)
?>
sig_array.php (exclusively for the array option)CODE
<?php
// Make $sig_aray an array:
$sig_array = array();
// Now add the pictures you want:
$sig_array[] = 'http://www.mysite.com/images/image1.jpg';
$sig_array[] = 'http://www.mysite.com/flower.jpg';
// To add your own pictures follow the format:
$sig_array[] = 'http://www.domain.com/full/path/to/image.extension';
// NOTE: it is a MUST that you include the FULL path to your image, INCLUDING the http://
?>
Once again, thanks for reading! Contact info can be found at the top!