Posted By

apphp-snippets on 02/20/13


Tagged

php password generation


Versions (?)

Automatic Password Generation in PHP


 / Published in: PHP
 

URL: http://www.apphp.com/index.php?snippet=php-generate-a-password

Sometimes you need to generate passwords for customers automatically when a new account is created. This code allows you choose the desired length and strength for the password and it is very flexible.

  1. <?php
  2. function GeneratePassword($length=8, $strength=0){
  3. $vowels = 'aeuy';
  4. $consonants = 'bdghjmnpqrstvz';
  5. if($strength >= 1) $consonants .= 'BDGHJLMNPQRSTVWXZ';
  6. if($strength >= 2) $vowels .= 'AEUY';
  7. if($strength >= 3) $consonants .= '12345';
  8. if($strength >= 4) $consonants .= '67890';
  9. if($strength >= 5) $vowels .= '@#$%';
  10.  
  11. $password = '';
  12. $alt = time() % 2;
  13. for($i = 0; $i < $length; $i++){
  14. if($alt == 1){
  15. $password .= $consonants[(rand() % strlen($consonants))];
  16. $alt = 0;
  17. }else{
  18. $password .= $vowels[(rand() % strlen($vowels))];
  19. $alt = 1;
  20. }
  21. }
  22. return $password;
  23. }
  24. ?>

Report this snippet  

You need to login to post a comment.