Discover & Rate New Music Check out ChartVote. Promote the music you like.

Create a “back” button with PHP

Deziner Folio posted this PHP snippet for creating a simple back link that will take you to the previous page you were on.

Code:

PHP:
  1. <?php session_start(); ?> <!– Starting a session before the DOCTYPE –>
  2.  
  3. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
  4. “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
  5.  
  6. <a href=”<?php echo $_SESSION[’back’]; ?>“>back</a> <!–The anchor tag that links to the previous page –>
  7.  
  8. <?php $_SESSION[’back’] = $_SERVER[’REQUEST_URI’]; ?> <!– Assigning the current URL to a session variable –>

Please note that first we assign the previous session variable to the anchor tag and only then re-assign the current URL to the session variable which will be read when you move on to the next page.

This being a simple PHP snippet can be used in almost every CMS (PHP based) and sure is a good addon to the accessibility of your site.

You can view the original article here

Random Images with PHP

Devlounge has posted an article for displaying random images with PHP.

Every now and then you want to randomize images, usually headers of sorts. This is pretty simple, with just 9 lines of PHP code you can rotate randomly between two images.

PHP:
  1. <?php
  2.     $images = array(
  3.         0 => 'image1.gif',
  4.         1 => 'image2.gif',
  5.     );
  6.     $image = $images[ rand(0,(count($images)-1)) ];
  7.     $output = "<img src=\"/mysite/randomimages/".$image."\" alt=\"\" border=\"0\" />";
  8.     print($output);
  9. ?>

This will rotate between image1.gif and image2.gif, which are located in /mysite/randomimages/ on your server.

Check out the full post here

Random Password Generator with PHP

This function will generate a random password based off of a character list that you specify. This is very useful for a user registration form.

Example: Instead of asking a user for their desired password on a sign up form, generate a random one for them and email it to them. This poses less of a security risk by emailing them a random password and less fields for them to to fill out when signing up.

PHP:
  1. function randomPassword($length = 7) {
  2.   // empty password
  3.   $password = "";
  4.   // define possible characters
  5.   $possible = "23456789abcdefghjkmnpqrstuvwxyz";
  6.   // add random characters until $length is reached
  7.   $i = 0;
  8.   while ($i <$length) { 
  9.     // pick a random character from the possible ones
  10.     $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  11.     // don't allow duplicate characters
  12.     if (strpos($password, $char) === false) {
  13.       $password .= $char;
  14.       $i++;
  15.     }
  16.   }
  17.   return $password;
  18. }

List all files in a directory with PHP

This code snippet displays a hyper linked list of all the files contained in a specified folder. I find myself always coming back to this very useful piece of code. I can't count how many times I've used this.

PHP:
  1. <?
  2.     // Define the full path to the folder whose contents you want to list
  3.     $path = "/home/user/public/foldername/";
  4.     // Open the directory
  5.     $dir_handle = @opendir($path) or die("Error opening $path");
  6.     // Loop through the files
  7.     while ($file = readdir($dir_handle)) {
  8.       if($file == "." || $file == ".." || $file == "index.php" ) { continue; }
  9.    
  10.       echo "<a href=\"$file\">$file</a><br />";
  11.     }
  12.     // Close
  13.     closedir($dir_handle);
  14. ?>

RSS Feed



Recommended Sites