Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2017
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.71 KB | None | 0 0
  1. function generate_code($length = 8) {
  2.            
  3.               // start with a blank password
  4.               $password = "";
  5.            
  6.               // define possible characters
  7.               $possible = "aAb0BcBdD2eEf3gGhHjJ4kKLmMnN5opPqQrRsS6tTu8UvVwW7xXyY9zZ";
  8.                
  9.               // set up a counter
  10.               $i = 0;
  11.                
  12.               // add random characters to $password until $length is reached
  13.               while ($i < $length) {
  14.            
  15.                 // pick a random character from the possible ones
  16.                 $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  17.                    
  18.                 // we don't want this character if it's already in the password
  19.                 if (!strstr($password, $char)) {
  20.                   $password .= $char;
  21.                   $i++;
  22.                 }
  23.            
  24.               }
  25.            
  26.               // done!
  27.               return $password;
  28.            
  29.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement