Posted By

mladoux on 09/21/10


Tagged

auth php password random security utility


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

widgetyegg


Simple Random Pass Generator


 / Published in: PHP
 

URL: http://mark.haktstudios.com/

A simple php function to generate random passwords between 8-14 characters long. Good for temporary passwords for use with a password recovery system as reset passwords.

  1. function gen_pass()
  2. {
  3. // load up our valid character string
  4. $chars = '01234567890';
  5. $chars .= 'abcdefghijklmnopqrstuvwxyz';
  6. $chars .= 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
  7. $chars .= '~!@#$%^&*()_+{}[]|/?<>,.';
  8.  
  9. // set password legnth between 8 and 14 characters long
  10. $len = rand(8, 14);
  11.  
  12. // initialize password & counter
  13. $pass = '';
  14. $i = 0;
  15.  
  16. while ($i < $len)
  17. {
  18. // get a new character
  19. $new_char = substr($chars, rand(0, strlen($chars) -1), 1);
  20.  
  21. // ensure all characters are different
  22. if(! strstr($pass, $new_char))
  23. {
  24. $pass .= $new_char;
  25. $i++;
  26. }
  27. }
  28.  
  29. return $pass;
  30. }

Report this snippet  

You need to login to post a comment.