Monday 23 February 2015

PHP: Automatic password creation

Although I personally prefer leaving users to choose their password themselves, a client recently asked me to generate passwords automatically when a new account is created.

The following function is flexible: You can choose the desired length and strength for the password.

<?php
function generatePassword($length=9, $strength=0) {
 $vowels = 'aeuy';
 $consonants = 'bdghjmnpqrstvz';
 if ($strength >= 1) {
  $consonants .= 'BDGHJLMNPQRSTVWXZ';
 }
 if ($strength >= 2) {
  $vowels .= "AEUY";
 }
 if ($strength >= 4) {
  $consonants .= '23456789';
 }
 if ($strength >= 8 ) {
  $vowels .= '@#$%';
 }

 $password = '';
 $alt = time() % 2;
 for ($i = 0; $i < $length; $i++) {
  if ($alt == 1) {
   $password .= $consonants[(rand() % strlen($consonants))];
   $alt = 0;
  } else {
   $password .= $vowels[(rand() % strlen($vowels))];
   $alt = 1;
  }
 }
 return $password;
}
?>

0 comments:

Post a Comment