Posted By

stz184 on 10/08/10


Tagged

password random generator


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

jplr


Random password generator


 / Published in: PHP
 

This function can be used to generate random passwords with specified length. Also, can use specified list of characters ($index) and generate passwords with only lower or only upper case characters.

  1. function random_password($lenght=6, $text_transform='uppercase') {
  2. $index = "abcdefghijklmnopqrstuvwxyz0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
  3. //build array
  4. for($i = 0; $i < mb_strlen($index); $i++) {
  5. $signs[] = mb_substr($index, $i, 1);
  6. }
  7. shuffle($signs);
  8. $password = '';
  9. for($i = 0; $i <= $lenght; $i++) {
  10. $password .= $signs[$i];
  11. }
  12. if($text_transform == 'lowercase') {
  13. return mb_strtolower($password);
  14. }
  15. elseif($text_transform == 'uppercase') {
  16. return mb_strtoupper($password);
  17. }
  18. else {
  19. return $password;
  20. }
  21. }

Report this snippet  

You need to login to post a comment.