Posted By

Meestercor on 05/22/08


Tagged

textmate password Pass generate wachtwoord


Versions (?)

Generate password


 / Published in: Other
 

  1. function generatePassword ($length = 16)
  2. {
  3.  
  4. // start with a blank password
  5. $password = "";
  6.  
  7. // define possible characters
  8. $possible = "0123456789bcdfghjkmnpqrstvwxyz";
  9.  
  10. // set up a counter
  11. $i = 0;
  12.  
  13. // add random characters to $password until $length is reached
  14. while ($i < $length) {
  15.  
  16. // pick a random character from the possible ones
  17. $char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
  18.  
  19. // we don't want this character if it's already in the password
  20. if (!strstr($password, $char)) {
  21. $password .= $char;
  22. $i++;
  23. }
  24.  
  25. }
  26.  
  27. // done!
  28. return $password;
  29.  
  30. }

Report this snippet  

Comments

RSS Icon Subscribe to comments
Posted By: AzizLight on June 27, 2010

There is a bug in your script:

if the length of the password (passed as argument) is greater than the number of possible characters, than you'll end up having an infinite loop!

You need to login to post a comment.