unique password|code generator


/ Published in: PHP
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.