Posted By

zogmund on 10/27/11


Tagged

php password generator unique


Versions (?)

Who likes this?

2 people have marked this snippet as a favorite

Net-Freak
tux-world


unique password|code generator


 / Published in: PHP
 

  1. function generatePasswords( $howMany = 10, $length = 6, $allowedChars = NULL )
  2. {
  3. if (NULL == $allowedChars) {
  4. $allowedChars = 'abcdefghijklmnopqrstuvwxyz0123456789';
  5. }
  6. $allowedChars = (string)$allowedChars;
  7. $allowedCharsLength = strlen( $allowedChars );
  8. $allowedCharsUpper = strtoupper( $allowedChars );
  9.  
  10. $pwds = array();
  11. $count = 0;
  12. while ($count < $howMany) {
  13. for ($j = $howMany - $count; $j--;) {
  14. $code = '';
  15. for ($i = $length; $i--;) {
  16. $rand = mt_rand(0, $allowedCharsLength - 1);
  17. if ($rand % 2) {
  18. $code .= $allowedChars{$rand};
  19. } else {
  20. $code .= $allowedCharsUpper{$rand};
  21. }
  22. }
  23. $pwds[$code] = '';
  24. }
  25. $count = count( $pwds );
  26. }
  27. return array_keys( $pwds );
  28. }

Report this snippet  

You need to login to post a comment.