Create Password in PHP


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



Copy this code and paste it in your HTML
  1. function create_password($pw_length = 8, $use_caps = true, $use_numeric = true, $use_specials = true) {
  2. $caps = array();
  3. $numbers = array();
  4. $num_specials = 0;
  5. $reg_length = $pw_length;
  6. $pws = array();
  7. for ($ch = 97; $ch <= 122; $ch++) $chars[] = $ch; // create a-z
  8. if ($use_caps) for ($ca = 65; $ca <= 90; $ca++) $caps[] = $ca; // create A-Z
  9. if ($use_numeric) for ($nu = 48; $nu <= 57; $nu++) $numbers[] = $nu; // create 0-9
  10. $all = array_merge($chars, $caps, $numbers);
  11. if ($use_specials) {
  12. $reg_length = ceil($pw_length*0.75);
  13. $num_specials = $pw_length - $reg_length;
  14. if ($num_specials > 5) $num_specials = 5;
  15. for ($si = 33; $si <= 47; $si++) $signs[] = $si;
  16. $rs_keys = array_rand($signs, $num_specials);
  17. foreach ($rs_keys as $rs) {
  18. $pws[] = chr($signs[$rs]);
  19. }
  20. }
  21. $rand_keys = array_rand($all, $reg_length);
  22. foreach ($rand_keys as $rand) {
  23. $pw[] = chr($all[$rand]);
  24. }
  25. $compl = array_merge($pw, $pws);
  26. shuffle($compl);
  27. return implode('', $compl);
  28. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.