General Statistics
390 Tutorials
202 Total Votes
10 User(s) Online
975111 Views Total
3000 Registered Members
24734 Posts Total
Rooneyfeld is our newest member
About this Tutorial: Advanced Random Signature Generator
Advanced Random Signature Generator
Rated 5 / 2 Votes

Advanced Random Signature Generator

Ever seen those cool random signature generators on forums? This script will teach you how to do that using PHP!


Date Added: April 19, 2006 [ 61675 Views - 2 Votes ] [ Rate ]

Author: Boost [ PHP Coding ] [ 4 Comment(s) ]

Advanced Random Signature Generator

Our script is going to have 2 options. The first one will have the system read a specified directory, look for images in it, and spit back a random one if it finds one.

Let's create the directory method first.

CODE
$choice = 'dir';
// If you want the array option set $choice = 'array';

// Make $images an array
$images = array();

if ($choice == 'dir') {
    $dir_path = 'images/sigs/';
  
    // Make sure $dir_path exists
    if (!is_dir($dir_path)) {
        exit($dir_path . 'does not exist. Please use a valid directory.');
    } else {
        // Open the directory:
        if ($dh = opendir($dir_path)) {
            // This is the correct way to loop over a directory
            while (($file = readdir($dh)) !== false) {
                // Get the extension of the file:
                $ext = strtolower(strrchr($file,'.'));
                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)
  
    $get_sig = rand(0, count($images)-1);
    $url = 'http://'.$domain.$path_use.$images[$get_sig];
    // Could be combined but, possibly more confusing too: $url = 'http://'.$domain.$path_use.$images[rand(0, count($images)-1)];

    // Forward script to the image:
    header('Location:  '.$url);

#### END DIRECTORY OPTION ####
####  BEGIN ARRAY OPTION  ####

} elseif ( $choice == 'array' ) {
// Array code here


This code is much simpler than it may seem. Most of it is already commented, but i will explain it here also. First, we ensure that $chioce is 'dir' if it isn't, the script will move on. Next we specify the location of the directory FROM THE FOLDER adv_rsig.php IS IN!! We check whether the directory exists, if it doesn't, we terminate the script, if it does, we move on to opening the directory.

Once the directory is open, we have to read it's contents. Because we have to do this for each file, we are required to loop over the directory, for every file we find. There is a right and wrong awy of donig this. There is a correct way and an incorrect way of doing this. The correct way is in the script, if you are curious to know the incorrect way, here it is:

CODE
while ($file = readdir($handle)) {
    // do stuff
}


The loop in the script is saying, while they are files, keep doing stuff...In our case, we take the file we are using and store it into $file. We check if $file is an image by using strrchar to get the file's extension. strrchar takes a 'needle' and looks for the last occurence of it and takes it to the end of the 'haystack'. In our case, we are looking for the last occurance of a '.' in $file because we need .extension. It is important to note that the extension that we found CONTAINS the '.' with the extension, that is why in our file_types array we included the . in front of the extensions.

readdir works a little funny. The first thing ti finds is the directory itself.. and the second thing it finds is .. which is the folder above (even if it's the root folder), like i said, it works a little funny. Well we obviously don't want to even bother with those, so we strip them out in our if statement that checks if $file is indeed a file, and if the extension is valid. If it is we use $images[] = $path_use . $dir_path . $file; to store the image into the $images array. values can be added to an array via $array_name[] = stuff. After we store ALL of the images into the $images array, because remember the loop is going over every file in the directory, we close the directory because we are done with it, and forward the script to the image.

We do this by using rand(min, max). In our script, we count the total number of values in the array (in other words, all of our images..) and take only 1 of them. We store that 1 image in $get_sig. Then we create our url to the image, and forward to it using header(). Notice how we used $images[$get_sig]. Arrays values can be called by their 'number' For example $array[2] will spit back the THIRD value in the array, because arrays ALWAYS start at 0. because of this, we ALWAYS subtract 1 from an aray when we count them.

This concludes our directory option. Hopefully you understood it, if you didn't see the contact info on the last page of the tutorial.

Now lets create the array option!

Total Pages (5)
<<Prev 1 2 [3] 4 5 Next>>

Welcome User
Welcome Guest
( Login | Register )

Latest Tutorial Index - RSS Feed
Latest Resources - RSS Feed
Latest Tutorials - RSS Feed
Vote for Us
  • VixxIMG.com - Free Image Hosting, Free Myspace Image Hosting, Free Image Uploads
  • PageRank Plus - Google PageRank checking, with a twist.
Random Affilaites