Random password generator


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

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.


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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.