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


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. }


Leave a Reply

RSS Feed



Recommended Sites